text:
package com.digitalpebble.stormcrawler.elasticsearch.persistence;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.storm.metric.api.IMetric;
import org.apache.storm.metric.api.MultiCountMetric;
import org.apache.storm.spout.SpoutOutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values;
import org.apache.storm.utils.Utils;
import org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsGroup;
import org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsRequest;
import org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse;
import org.elasticsearch.client.Client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.digitalpebble.stormcrawler.Metadata;
import com.digitalpebble.stormcrawler.elasticsearch.ElasticSearchConnection;
import com.digitalpebble.stormcrawler.util.CollectionMetric;
import com.digitalpebble.stormcrawler.util.ConfUtils;
import com.google.common.base.Optional;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

public abstract class AbstractSpout extends BaseRichSpout {

    private static final Logger LOG = LoggerFactory
            .getLogger(AbstractSpout.class);

    protected static final String ESBoltType = "status";
    protected static final String ESStatusIndexNameParamName = "es.status.index.name";
    protected static final String ESStatusDocTypeParamName = "es.status.doc.type";

    /**
     * Time in seconds for which acked or failed URLs will be considered for
     * fetching again, default 30 secs.
     **/
    protected static final String ESStatusTTLPurgatory = "es.status.ttl.purgatory";

    /** Field name to use for aggregating **/
    protected static final String ESStatusBucketFieldParamName = "es.status.bucket.field";
    protected static final String ESStatusMaxBucketParamName = "es.status.max.buckets";
    protected static final String ESStatusMaxURLsParamName = "es.status.max.urls.per.bucket";

    /**
     * Field name to use for sorting the URLs within a bucket, not used if empty
     * or null.
     **/
    protected static final String ESStatusBucketSortFieldParamName = "es.status.bucket.sort.field";

    /**
     * Field name to use for sorting the buckets, not used if empty or null.
     **/
    protected static final String ESStatusGlobalSortFieldParamName = "es.status.global.sort.field";

    /**
     * Min time to allow between 2 successive queries to ES. Value in msecs,
     * default 2000.
     **/
    private static final String ESStatusMinDelayParamName = "es.status.min.delay.queries";

    protected static final String ESStatusResetFetchDateParamName = "es.status.reset.fetchdate.after";

    protected static final String ESStatusFilterParamName = "es.status.filterQuery";

    protected String filterQuery = null;

    protected String indexName;
    protected String docType;
    protected boolean active = true;
    protected SpoutOutputCollector _collector;
    protected MultiCountMetric eventCounter;

    protected static Client client;

    /**
     * when using multiple instances - each one is in charge of a specific shard
     * useful when sharding based on host or domain to guarantee a good mix of
     * URLs
     */
    protected int shardID = -1;

    /** Used to distinguish between instances in the logs **/
    protected String logIdprefix = "";

    protected Queue<Values> buffer = new LinkedList<>();

    /**
     * Map to keep in-process URLs, ev. with additional information for URL /
     * politeness bucket (hostname / domain etc.). The entries are kept in a
     * cache for a configurable amount of time to avoid that some items are
     * fetched a second time if new items are queried shortly after they have
     * been acked.
     */
    protected InProcessMap<String, String> beingProcessed;

    protected long timeStartESQuery = 0;

    private long minDelayBetweenQueries = 2000;

    protected AtomicBoolean isInESQuery = new AtomicBoolean(false);

    /** Field name used for field collapsing e.g. metadata.hostname **/
    protected String partitionField;

    protected int maxURLsPerBucket = 10;

    protected int maxBucketNum = 10;

    protected String bucketSortField = "";

    protected String totalSortField = "";

    protected CollectionMetric esQueryTimes;

    protected Date lastDate;

    protected int resetFetchDateAfterNSecs = -1;

    /** Map which holds elements some additional time after the removal. */
    public class InProcessMap<K, V> extends HashMap<K, V> {

        private Cache<K, Optional<V>> deletionCache;

        public InProcessMap(long maxDuration, TimeUnit timeUnit) {
            deletionCache = CacheBuilder.newBuilder()
                    .expireAfterWrite(maxDuration, timeUnit).build();
        }

        @Override
        public boolean containsKey(Object key) {
            boolean incache = super.containsKey(key);
            if (!incache) {
                incache = (deletionCache.getIfPresent(key) != null);
            }
            return incache;
        }

        @Override
        public V remove(Object key) {
            deletionCache.put((K) key, Optional.absent());
            return super.remove(key);
        }

        public long inCache() {
            return deletionCache.size();
        }
    }

    @Override
    public void open(Map stormConf, TopologyContext context,
            SpoutOutputCollector collector) {

        indexName = ConfUtils.getString(stormConf, ESStatusIndexNameParamName,
                "status");
        docType = ConfUtils.getString(stormConf, ESStatusDocTypeParamName,
                "status");

        // one ES client per JVM
        synchronized (AbstractSpout.class) {
            try {
                if (client == null) {
                    client = ElasticSearchConnection.getClient(stormConf,
                            ESBoltType);
                }
            } catch (Exception e1) {
                LOG.error("Can't connect to ElasticSearch", e1);
                throw new RuntimeException(e1);
            }
        }

        // if more than one instance is used we expect their number to be the
        // same as the number of shards
        int totalTasks = context
                .getComponentTasks(context.getThisComponentId()).size();
        if (totalTasks > 1) {
            logIdprefix = "[" + context.getThisComponentId() + " #"
                    + context.getThisTaskIndex() + "] ";

            // determine the number of shards so that we can restrict the
            // search
            ClusterSearchShardsRequest request = new ClusterSearchShardsRequest(
                    indexName);
            ClusterSearchShardsResponse shardresponse = client.admin()
                    .cluster().searchShards(request).actionGet();
            ClusterSearchShardsGroup[] shardgroups = shardresponse.getGroups();
            if (totalTasks != shardgroups.length) {
                throw new RuntimeException(
                        "Number of ES spout instances should be the same as number of shards ("
                                + shardgroups.length + ") but is " + totalTasks);
            }
            shardID = shardgroups[context.getThisTaskIndex()].getShardId()
                    .getId();
            LOG.info("{} assigned shard ID {}", logIdprefix, shardID);
        }

        _collector = collector;

        int ttlPurgatory = ConfUtils
                .getInt(stormConf, ESStatusTTLPurgatory, 30);

        minDelayBetweenQueries = ConfUtils.getLong(stormConf,
                ESStatusMinDelayParamName, 2000);

        partitionField = ConfUtils.getString(stormConf,
                ESStatusBucketFieldParamName, "_routing");

        bucketSortField = ConfUtils.getString(stormConf,
                ESStatusBucketSortFieldParamName, bucketSortField);

        totalSortField = ConfUtils.getString(stormConf,
                ESStatusGlobalSortFieldParamName);

        maxURLsPerBucket = ConfUtils.getInt(stormConf,
                ESStatusMaxURLsParamName, 1);
        maxBucketNum = ConfUtils.getInt(stormConf, ESStatusMaxBucketParamName,
                10);

        resetFetchDateAfterNSecs = ConfUtils.getInt(stormConf,
                ESStatusResetFetchDateParamName, resetFetchDateAfterNSecs);

        beingProcessed = new InProcessMap<>(ttlPurgatory, TimeUnit.SECONDS);

        filterQuery = ConfUtils.getString(stormConf, ESStatusFilterParamName);

        eventCounter = context.registerMetric("counters",
                new MultiCountMetric(), 10);

        context.registerMetric("buffer_size", new IMetric() {
            @Override
            public Object getValueAndReset() {
                return buffer.size();
            }
        }, 10);

        context.registerMetric("beingProcessed", new IMetric() {
            @Override
            public Object getValueAndReset() {
                return beingProcessed.size();
            }
        }, 10);

        context.registerMetric("inPurgatory", new IMetric() {
            @Override
            public Object getValueAndReset() {
                return beingProcessed.inCache();
            }
        }, 10);

        esQueryTimes = new CollectionMetric();
        context.registerMetric("ES_query_time_msec", esQueryTimes, 10);
    }

    /** Returns true if ES was queried too recently and needs throttling **/
    protected boolean throttleESQueries() {
        Date now = new Date();
        if (timeStartESQuery != 0) {
            // check that we allowed some time between queries
            long difference = now.getTime() - timeStartESQuery;
            if (difference < minDelayBetweenQueries) {
                long sleepTime = minDelayBetweenQueries - difference;
                LOG.debug(
                        "{} Not enough time elapsed since {} - should try again in {}",
                        logIdprefix, timeStartESQuery, sleepTime);
                return true;
            }
        }
        return false;
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("url", "metadata"));
    }

    @Override
    public void nextTuple() {

        // inactive?
        if (active == false)
            return;

        synchronized (buffer) {
            // have anything in the buffer?
            if (!buffer.isEmpty()) {
                Values fields = buffer.remove();
                String url = fields.get(0).toString();
                beingProcessed.put(url, null);
                _collector.emit(fields, url);
                eventCounter.scope("emitted").incrBy(1);
                return;
            }
        }

        // check that we allowed some time between queries
        // and not in middle of querying ES
        if (isInESQuery.get() || throttleESQueries()) {
            // sleep for a bit but not too much in order to give ack/fail a
            // chance
            Utils.sleep(10);
            return;
        }

        // re-populate the buffer
        populateBuffer();
    }

    /** Builds a query and use it retrieve the results from ES **/
    protected abstract void populateBuffer();

    protected final Metadata fromKeyValues(Map<String, Object> keyValues) {
        Map<String, List<String>> mdAsMap = (Map<String, List<String>>) keyValues
                .get("metadata");
        Metadata metadata = new Metadata();
        if (mdAsMap != null) {
            Iterator<Entry<String, List<String>>> mdIter = mdAsMap.entrySet()
                    .iterator();
            while (mdIter.hasNext()) {
                Entry<String, List<String>> mdEntry = mdIter.next();
                String key = mdEntry.getKey();
                // periods are not allowed in ES2 - replace with %2E
                key = key.replaceAll("%2E", "\\.");
                Object mdValObj = mdEntry.getValue();
                // single value
                if (mdValObj instanceof String) {
                    metadata.addValue(key, (String) mdValObj);
                }
                // multi valued
                else {
                    metadata.addValues(key, (List<String>) mdValObj);
                }
            }
        }
        return metadata;
    }

    @Override
    public void ack(Object msgId) {
        LOG.debug("{}  Ack for {}", logIdprefix, msgId);
        beingProcessed.remove(msgId);
        eventCounter.scope("acked").incrBy(1);
    }

    @Override
    public void fail(Object msgId) {
        LOG.info("{}  Fail for {}", logIdprefix, msgId);
        beingProcessed.remove(msgId);
        eventCounter.scope("failed").incrBy(1);
    }

    @Override
    public void activate() {
        active = true;
    }

    @Override
    public void deactivate() {
        active = false;
    }

    @Override
    public void close() {
        if (client != null)
            client.close();
    }

}


text:
#include "config.h"
#include "ResourceHandle.h"

#include "CachedResourceLoader.h"
#include "ChromeClient.h"
#include "CookieJarSoup.h"
#include "CredentialStorage.h"
#include "FileSystem.h"
#include "Frame.h"
#include "GOwnPtrSoup.h"
#include "HTTPParsers.h"
#include "LocalizedStrings.h"
#include "Logging.h"
#include "MIMETypeRegistry.h"
#include "NotImplemented.h"
#include "Page.h"
#include "ResourceError.h"
#include "ResourceHandleClient.h"
#include "ResourceHandleInternal.h"
#include "ResourceResponse.h"
#include "SharedBuffer.h"
#include "TextEncoding.h"
#include <errno.h>
#include <fcntl.h>
#include <gio/gio.h>
#include <glib.h>
#define LIBSOUP_USE_UNSTABLE_REQUEST_API
#include <libsoup/soup-request-http.h>
#include <libsoup/soup-requester.h>
#include <libsoup/soup.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <wtf/SHA1.h>
#include <wtf/gobject/GRefPtr.h>
#include <wtf/text/Base64.h>
#include <wtf/text/CString.h>

#if ENABLE(BLOB)
#include "BlobData.h"
#include "BlobRegistryImpl.h"
#include "BlobStorageData.h"
#endif

#if PLATFORM(GTK)
#include "CredentialBackingStore.h"
#endif

namespace WebCore {

#define READ_BUFFER_SIZE 8192

static bool loadingSynchronousRequest = false;

class WebCoreSynchronousLoader : public ResourceHandleClient {
    WTF_MAKE_NONCOPYABLE(WebCoreSynchronousLoader);
public:

    WebCoreSynchronousLoader(ResourceError& error, ResourceResponse& response, SoupSession* session, Vector<char>& data)
        : m_error(error)
        , m_response(response)
        , m_session(session)
        , m_data(data)
        , m_finished(false)
    {
        // We don't want any timers to fire while we are doing our synchronous load
        // so we replace the thread default main context. The main loop iterations
        // will only process GSources associated with this inner context.
        loadingSynchronousRequest = true;
        GRefPtr<GMainContext> innerMainContext = adoptGRef(g_main_context_new());
        g_main_context_push_thread_default(innerMainContext.get());
        m_mainLoop = adoptGRef(g_main_loop_new(innerMainContext.get(), false));

        adjustMaxConnections(1);
    }

    ~WebCoreSynchronousLoader()
    {
        adjustMaxConnections(-1);
        g_main_context_pop_thread_default(g_main_context_get_thread_default());
        loadingSynchronousRequest = false;
    }

    void adjustMaxConnections(int adjustment)
    {
        int maxConnections, maxConnectionsPerHost;
        g_object_get(m_session,
                     SOUP_SESSION_MAX_CONNS, &maxConnections,
                     SOUP_SESSION_MAX_CONNS_PER_HOST, &maxConnectionsPerHost,
                     NULL);
        maxConnections += adjustment;
        maxConnectionsPerHost += adjustment;
        g_object_set(m_session,
                     SOUP_SESSION_MAX_CONNS, maxConnections,
                     SOUP_SESSION_MAX_CONNS_PER_HOST, maxConnectionsPerHost,
                     NULL);

    }

    virtual bool isSynchronousClient()
    {
        return true;
    }

    virtual void didReceiveResponse(ResourceHandle*, const ResourceResponse& response)
    {
        m_response = response;
    }

    virtual void didReceiveData(ResourceHandle*, const char* data, int length, int)
    {
        m_data.append(data, length);
    }

    virtual void didFinishLoading(ResourceHandle*, double)
    {
        if (g_main_loop_is_running(m_mainLoop.get()))
            g_main_loop_quit(m_mainLoop.get());
        m_finished = true;
    }

    virtual void didFail(ResourceHandle* handle, const ResourceError& error)
    {
        m_error = error;
        didFinishLoading(handle, 0);
    }

    virtual void didReceiveAuthenticationChallenge(ResourceHandle*, const AuthenticationChallenge& challenge)
    {
        // We do not handle authentication for synchronous XMLHttpRequests.
        challenge.authenticationClient()->receivedRequestToContinueWithoutCredential(challenge);
    }

    void run()
    {
        if (!m_finished)
            g_main_loop_run(m_mainLoop.get());
    }

private:
    ResourceError& m_error;
    ResourceResponse& m_response;
    SoupSession* m_session;
    Vector<char>& m_data;
    bool m_finished;
    GRefPtr<GMainLoop> m_mainLoop;
};

class HostTLSCertificateSet {
public:
    void add(GTlsCertificate* certificate)
    {
        String certificateHash = computeCertificateHash(certificate);
        if (!certificateHash.isEmpty())
            m_certificates.add(certificateHash);
    }

    bool contains(GTlsCertificate* certificate)
    {
        return m_certificates.contains(computeCertificateHash(certificate));
    }

private:
    static String computeCertificateHash(GTlsCertificate* certificate)
    {
        GByteArray* data = 0;
        g_object_get(G_OBJECT(certificate), "certificate", &data, NULL);
        if (!data)
            return String();

        static const size_t sha1HashSize = 20;
        GRefPtr<GByteArray> certificateData = adoptGRef(data);
        SHA1 sha1;
        sha1.addBytes(certificateData->data, certificateData->len);

        Vector<uint8_t, sha1HashSize> digest;
        sha1.computeHash(digest);

        return base64Encode(reinterpret_cast<const char*>(digest.data()), sha1HashSize);
    }

    HashSet<String> m_certificates;
};

static void cleanupSoupRequestOperation(ResourceHandle*, bool isDestroying);
static void sendRequestCallback(GObject*, GAsyncResult*, gpointer);
static void readCallback(GObject*, GAsyncResult*, gpointer);
static void closeCallback(GObject*, GAsyncResult*, gpointer);
static gboolean requestTimeoutCallback(void*);
#if ENABLE(WEB_TIMING)
static int  milisecondsSinceRequest(double requestTime);
#endif

static bool gIgnoreSSLErrors = false;

static HashSet<String>& allowsAnyHTTPSCertificateHosts()
{
    DEFINE_STATIC_LOCAL(HashSet<String>, hosts, ());
    return hosts;
}

typedef HashMap<String, HostTLSCertificateSet> CertificatesMap;
static CertificatesMap& clientCertificates()
{
    DEFINE_STATIC_LOCAL(CertificatesMap, certificates, ());
    return certificates;
}

ResourceHandleInternal::~ResourceHandleInternal()
{
}

static SoupSession* sessionFromContext(NetworkingContext* context)
{
    return (context && context->isValid()) ? context->soupSession() : ResourceHandle::defaultSession();
}

ResourceHandle::~ResourceHandle()
{
    cleanupSoupRequestOperation(this, true);
}

static void ensureSessionIsInitialized(SoupSession* session)
{
    if (g_object_get_data(G_OBJECT(session), "webkit-init"))
        return;

    if (session == ResourceHandle::defaultSession()) {
        SoupCookieJar* jar = SOUP_COOKIE_JAR(soup_session_get_feature(session, SOUP_TYPE_COOKIE_JAR));
        if (!jar)
            soup_session_add_feature(session, SOUP_SESSION_FEATURE(soupCookieJar()));
        else
            setSoupCookieJar(jar);
    }

#if !LOG_DISABLED
    if (!soup_session_get_feature(session, SOUP_TYPE_LOGGER) && LogNetwork.state == WTFLogChannelOn) {
        SoupLogger* logger = soup_logger_new(static_cast<SoupLoggerLogLevel>(SOUP_LOGGER_LOG_BODY), -1);
        soup_session_add_feature(session, SOUP_SESSION_FEATURE(logger));
        g_object_unref(logger);
    }
#endif // !LOG_DISABLED

    if (!soup_session_get_feature(session, SOUP_TYPE_REQUESTER)) {
        SoupRequester* requester = soup_requester_new();
        soup_session_add_feature(session, SOUP_SESSION_FEATURE(requester));
        g_object_unref(requester);
    }

    g_object_set_data(G_OBJECT(session), "webkit-init", reinterpret_cast<void*>(0xdeadbeef));
}

SoupSession* ResourceHandleInternal::soupSession()
{
    SoupSession* session = sessionFromContext(m_context.get());
    ensureSessionIsInitialized(session);
    return session;
}

static void gotHeadersCallback(SoupMessage* message, gpointer data)
{
    ResourceHandle* handle = static_cast<ResourceHandle*>(data);
    if (!handle)
        return;
    ResourceHandleInternal* d = handle->getInternal();
    if (d->m_cancelled)
        return;

#if ENABLE(WEB_TIMING)
    if (d->m_response.resourceLoadTiming())
        d->m_response.resourceLoadTiming()->receiveHeadersEnd = milisecondsSinceRequest(d->m_response.resourceLoadTiming()->requestTime);
#endif

#if PLATFORM(GTK)
    // We are a bit more conservative with the persistent credential storage than the session store,
    // since we are waiting until we know that this authentication succeeded before actually storing.
    // This is because we want to avoid hitting the disk twice (once to add and once to remove) for
    // incorrect credentials or polluting the keychain with invalid credentials.
    if (message->status_code != 401 && message->status_code < 500 && !d->m_credentialDataToSaveInPersistentStore.credential.isEmpty()) {
        credentialBackingStore().storeCredentialsForChallenge(
            d->m_credentialDataToSaveInPersistentStore.challenge,
            d->m_credentialDataToSaveInPersistentStore.credential);
    }
#endif

    // The original response will be needed later to feed to willSendRequest in
    // restartedCallback() in case we are redirected. For this reason, so we store it
    // here.
    ResourceResponse response;
    response.updateFromSoupMessage(message);
    d->m_response = response;
}

static void applyAuthenticationToRequest(ResourceHandle* handle, bool redirect)
{
    // m_user/m_pass are credentials given manually, for instance, by the arguments passed to XMLHttpRequest.open().
    ResourceHandleInternal* d = handle->getInternal();
    String user = d->m_user;
    String password = d->m_pass;

    ResourceRequest& request = d->m_firstRequest;
    if (handle->shouldUseCredentialStorage()) {
        if (d->m_user.isEmpty() && d->m_pass.isEmpty())
            d->m_initialCredential = CredentialStorage::get(request.url());
        else if (!redirect) {
            // If there is already a protection space known for the URL, update stored credentials
            // before sending a request. This makes it possible to implement logout by sending an
            // XMLHttpRequest with known incorrect credentials, and aborting it immediately (so that
            // an authentication dialog doesn't pop up).
            CredentialStorage::set(Credential(d->m_user, d->m_pass, CredentialPersistenceNone), request.url());
        }
    }

    if (!d->m_initialCredential.isEmpty()) {
        user = d->m_initialCredential.user();
        password = d->m_initialCredential.password();
    }

    // We always put the credentials into the URL. In the CFNetwork-port HTTP family credentials are applied in
    // the didReceiveAuthenticationChallenge callback, but libsoup requires us to use this method to override
    // any previously remembered credentials. It has its own per-session credential storage.
    if (!user.isEmpty() || !password.isEmpty()) {
        KURL urlWithCredentials(request.url());
        urlWithCredentials.setUser(d->m_user);
        urlWithCredentials.setPass(d->m_pass);
        request.setURL(urlWithCredentials);
    }
}

// Called each time the message is going to be sent again except the first time.
// It's used mostly to let webkit know about redirects.
static void restartedCallback(SoupMessage* message, gpointer data)
{
    ResourceHandle* handle = static_cast<ResourceHandle*>(data);
    if (!handle)
        return;
    ResourceHandleInternal* d = handle->getInternal();
    if (d->m_cancelled)
        return;

    GOwnPtr<char> uri(soup_uri_to_string(soup_message_get_uri(message), false));
    String location = String::fromUTF8(uri.get());
    KURL newURL = KURL(handle->firstRequest().url(), location);

    ResourceRequest request = handle->firstRequest();
    request.setURL(newURL);
    request.setHTTPMethod(message->method);

    // Should not set Referer after a redirect from a secure resource to non-secure one.
    if (!request.url().protocolIs("https") && protocolIs(request.httpReferrer(), "https")) {
        request.clearHTTPReferrer();
        soup_message_headers_remove(message->request_headers, "Referer");
    }

    const KURL& url = request.url();
    d->m_user = url.user();
    d->m_pass = url.pass();
    request.removeCredentials();

    ResourceResponse& redirectResponse = d->m_response;
    if (!protocolHostAndPortAreEqual(request.url(), redirectResponse.url())) {
        // If the network layer carries over authentication headers from the original request
        // in a cross-origin redirect, we want to clear those headers here.
        request.clearHTTPAuthorization();
        soup_message_headers_remove(message->request_headers, "Authorization");

        // TODO: We are losing any username and password specified in the redirect URL, as this is the
        // same behavior as the CFNet port. We should investigate if this is really what we want.
    } else
        applyAuthenticationToRequest(handle, true);

    // Per-request authentication is handled via the URI-embedded username/password.
    GOwnPtr<SoupURI> newSoupURI(soup_uri_new(request.urlStringForSoup().utf8().data()));
    soup_message_set_uri(message, newSoupURI.get());

    if (d->client())
        d->client()->willSendRequest(handle, request, redirectResponse);

    if (d->m_cancelled)
        return;

#if ENABLE(WEB_TIMING)
    redirectResponse.setResourceLoadTiming(ResourceLoadTiming::create());
    redirectResponse.resourceLoadTiming()->requestTime = monotonicallyIncreasingTime();
#endif

    // Update the first party in case the base URL changed with the redirect
    String firstPartyString = request.firstPartyForCookies().string();
    if (!firstPartyString.isEmpty()) {
        GOwnPtr<SoupURI> firstParty(soup_uri_new(firstPartyString.utf8().data()));
        soup_message_set_first_party(d->m_soupMessage.get(), firstParty.get());
    }
}

static void wroteBodyDataCallback(SoupMessage*, SoupBuffer* buffer, gpointer data)
{
    RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
    if (!handle)
        return;

    ASSERT(buffer);
    ResourceHandleInternal* internal = handle->getInternal();
    internal->m_bodyDataSent += buffer->length;

    if (internal->m_cancelled)
        return;
    ResourceHandleClient* client = handle->client();
    if (!client)
        return;

    client->didSendData(handle.get(), internal->m_bodyDataSent, internal->m_bodySize);
}

static void cleanupSoupRequestOperation(ResourceHandle* handle, bool isDestroying = false)
{
    ResourceHandleInternal* d = handle->getInternal();

    if (d->m_soupRequest)
        d->m_soupRequest.clear();

    if (d->m_inputStream)
        d->m_inputStream.clear();

    d->m_cancellable.clear();

    if (d->m_soupMessage) {
        g_signal_handlers_disconnect_matched(d->m_soupMessage.get(), G_SIGNAL_MATCH_DATA,
                                             0, 0, 0, 0, handle);
        g_object_set_data(G_OBJECT(d->m_soupMessage.get()), "handle", 0);
        d->m_soupMessage.clear();
    }

    if (d->m_buffer) {
        g_slice_free1(READ_BUFFER_SIZE, d->m_buffer);
        d->m_buffer = 0;
    }

    if (d->m_timeoutSource) {
        g_source_destroy(d->m_timeoutSource.get());
        d->m_timeoutSource.clear();
    }

    if (!isDestroying)
        handle->deref();
}

static bool handleUnignoredTLSErrors(ResourceHandle* handle)
{
    ResourceHandleInternal* d = handle->getInternal();
    const ResourceResponse& response = d->m_response;

    if (!response.soupMessageTLSErrors() || gIgnoreSSLErrors)
        return false;

    String lowercaseHostURL = handle->firstRequest().url().host().lower();
    if (allowsAnyHTTPSCertificateHosts().contains(lowercaseHostURL))
        return false;

    // We aren't ignoring errors globally, but the user may have already decided to accept this certificate.
    CertificatesMap::iterator i = clientCertificates().find(lowercaseHostURL);
    if (i != clientCertificates().end() && i->value.contains(response.soupMessageCertificate()))
        return false;

    handle->client()->didFail(handle, ResourceError::tlsError(d->m_soupRequest.get(), response.soupMessageTLSErrors(), response.soupMessageCertificate()));
    return true;
}

static void sendRequestCallback(GObject*, GAsyncResult* res, gpointer data)
{
    RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);

    ResourceHandleInternal* d = handle->getInternal();
    ResourceHandleClient* client = handle->client();
    SoupMessage* soupMessage = d->m_soupMessage.get();

    if (d->m_cancelled || !client) {
        cleanupSoupRequestOperation(handle.get());
        return;
    }

    if (d->m_defersLoading) {
        d->m_deferredResult = res;
        return;
    }

    GOwnPtr<GError> error;
    GInputStream* in = soup_request_send_finish(d->m_soupRequest.get(), res, &error.outPtr());
    if (error) {
        client->didFail(handle.get(), ResourceError::httpError(soupMessage, error.get(), d->m_soupRequest.get()));
        cleanupSoupRequestOperation(handle.get());
        return;
    }

    d->m_inputStream = adoptGRef(in);
    d->m_buffer = static_cast<char*>(g_slice_alloc(READ_BUFFER_SIZE));

    if (soupMessage) {
        if (handle->shouldContentSniff() && soupMessage->status_code != SOUP_STATUS_NOT_MODIFIED) {
            const char* sniffedType = soup_request_get_content_type(d->m_soupRequest.get());
            d->m_response.setSniffedContentType(sniffedType);
        }
        d->m_response.updateFromSoupMessage(soupMessage);

        if (handleUnignoredTLSErrors(handle.get())) {
            cleanupSoupRequestOperation(handle.get());
            return;
        }

    } else {
        d->m_response.setURL(handle->firstRequest().url());
        const gchar* contentType = soup_request_get_content_type(d->m_soupRequest.get());
        d->m_response.setMimeType(extractMIMETypeFromMediaType(contentType));
        d->m_response.setTextEncodingName(extractCharsetFromMediaType(contentType));
        d->m_response.setExpectedContentLength(soup_request_get_content_length(d->m_soupRequest.get()));
    }

    client->didReceiveResponse(handle.get(), d->m_response);

    if (d->m_cancelled) {
        cleanupSoupRequestOperation(handle.get());
        return;
    }

    g_input_stream_read_async(d->m_inputStream.get(), d->m_buffer, READ_BUFFER_SIZE,
                              G_PRIORITY_DEFAULT, d->m_cancellable.get(), readCallback, handle.get());
}

static bool addFileToSoupMessageBody(SoupMessage* message, const String& fileNameString, size_t offset, size_t lengthToSend, unsigned long& totalBodySize)
{
    GOwnPtr<GError> error;
    CString fileName = fileSystemRepresentation(fileNameString);
    GMappedFile* fileMapping = g_mapped_file_new(fileName.data(), false, &error.outPtr());
    if (error)
        return false;

    gsize bufferLength = lengthToSend;
    if (!lengthToSend)
        bufferLength = g_mapped_file_get_length(fileMapping);
    totalBodySize += bufferLength;

    SoupBuffer* soupBuffer = soup_buffer_new_with_owner(g_mapped_file_get_contents(fileMapping) + offset,
                                                        bufferLength,
                                                        fileMapping,
                                                        reinterpret_cast<GDestroyNotify>(g_mapped_file_unref));
    soup_message_body_append_buffer(message->request_body, soupBuffer);
    soup_buffer_free(soupBuffer);
    return true;
}

#if ENABLE(BLOB)
static bool blobIsOutOfDate(const BlobDataItem& blobItem)
{
    ASSERT(blobItem.type == BlobDataItem::File);
    if (!isValidFileTime(blobItem.expectedModificationTime))
        return false;

    time_t fileModificationTime;
    if (!getFileModificationTime(blobItem.path, fileModificationTime))
        return true;

    return fileModificationTime != static_cast<time_t>(blobItem.expectedModificationTime);
}

static void addEncodedBlobItemToSoupMessageBody(SoupMessage* message, const BlobDataItem& blobItem, unsigned long& totalBodySize)
{
    if (blobItem.type == BlobDataItem::Data) {
        totalBodySize += blobItem.length;
        soup_message_body_append(message->request_body, SOUP_MEMORY_TEMPORARY,
                                 blobItem.data->data() + blobItem.offset, blobItem.length);
        return;
    }

    ASSERT(blobItem.type == BlobDataItem::File);
    if (blobIsOutOfDate(blobItem))
        return;

    addFileToSoupMessageBody(message,
                             blobItem.path,
                             blobItem.offset,
                             blobItem.length == BlobDataItem::toEndOfFile ? 0 : blobItem.length,
                             totalBodySize);
}

static void addEncodedBlobToSoupMessageBody(SoupMessage* message, const FormDataElement& element, unsigned long& totalBodySize)
{
    RefPtr<BlobStorageData> blobData = static_cast<BlobRegistryImpl&>(blobRegistry()).getBlobDataFromURL(KURL(ParsedURLString, element.m_url));
    if (!blobData)
        return;

    for (size_t i = 0; i < blobData->items().size(); ++i)
        addEncodedBlobItemToSoupMessageBody(message, blobData->items()[i], totalBodySize);
}
#endif // ENABLE(BLOB)

static bool addFormElementsToSoupMessage(SoupMessage* message, const char*, FormData* httpBody, unsigned long& totalBodySize)
{
    soup_message_body_set_accumulate(message->request_body, FALSE);
    size_t numElements = httpBody->elements().size();
    for (size_t i = 0; i < numElements; i++) {
        const FormDataElement& element = httpBody->elements()[i];

        if (element.m_type == FormDataElement::data) {
            totalBodySize += element.m_data.size();
            soup_message_body_append(message->request_body, SOUP_MEMORY_TEMPORARY,
                                     element.m_data.data(), element.m_data.size());
            continue;
        }

        if (element.m_type == FormDataElement::encodedFile) {
            if (!addFileToSoupMessageBody(message ,
                                         element.m_filename,
                                         0 /* offset */,
                                         0 /* lengthToSend */,
                                         totalBodySize))
                return false;
            continue;
        }

#if ENABLE(BLOB)
        ASSERT(element.m_type == FormDataElement::encodedBlob);
        addEncodedBlobToSoupMessageBody(message, element, totalBodySize);
#endif
    }
    return true;
}

#if ENABLE(WEB_TIMING)
static int milisecondsSinceRequest(double requestTime)
{
    return static_cast<int>((monotonicallyIncreasingTime() - requestTime) * 1000.0);
}

static void wroteBodyCallback(SoupMessage*, gpointer data)
{
    RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
    if (!handle)
        return;

    ResourceHandleInternal* d = handle->getInternal();
    if (!d->m_response.resourceLoadTiming())
        return;

    d->m_response.resourceLoadTiming()->sendEnd = milisecondsSinceRequest(d->m_response.resourceLoadTiming()->requestTime);
}

static void requestStartedCallback(SoupSession*, SoupMessage* soupMessage, SoupSocket*, gpointer)
{
    RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(g_object_get_data(G_OBJECT(soupMessage), "handle"));
    if (!handle)
        return;

    ResourceHandleInternal* d = handle->getInternal();
    if (!d->m_response.resourceLoadTiming())
        return;

    d->m_response.resourceLoadTiming()->sendStart = milisecondsSinceRequest(d->m_response.resourceLoadTiming()->requestTime);
    if (d->m_response.resourceLoadTiming()->sslStart != -1) {
        // WebCore/inspector/front-end/RequestTimingView.js assumes
        // that SSL time is included in connection time so must
        // substract here the SSL delta that will be added later (see
        // WebInspector.RequestTimingView.createTimingTable in the
        // file above for more details).
        d->m_response.resourceLoadTiming()->sendStart -=
            d->m_response.resourceLoadTiming()->sslEnd - d->m_response.resourceLoadTiming()->sslStart;
    }
}

static void networkEventCallback(SoupMessage*, GSocketClientEvent event, GIOStream*, gpointer data)
{
    ResourceHandle* handle = static_cast<ResourceHandle*>(data);
    if (!handle)
        return;
    ResourceHandleInternal* d = handle->getInternal();
    if (d->m_cancelled)
        return;

    int deltaTime = milisecondsSinceRequest(d->m_response.resourceLoadTiming()->requestTime);
    switch (event) {
    case G_SOCKET_CLIENT_RESOLVING:
        d->m_response.resourceLoadTiming()->dnsStart = deltaTime;
        break;
    case G_SOCKET_CLIENT_RESOLVED:
        d->m_response.resourceLoadTiming()->dnsEnd = deltaTime;
        break;
    case G_SOCKET_CLIENT_CONNECTING:
        d->m_response.resourceLoadTiming()->connectStart = deltaTime;
        if (d->m_response.resourceLoadTiming()->dnsStart != -1)
            // WebCore/inspector/front-end/RequestTimingView.js assumes
            // that DNS time is included in connection time so must
            // substract here the DNS delta that will be added later (see
            // WebInspector.RequestTimingView.createTimingTable in the
            // file above for more details).
            d->m_response.resourceLoadTiming()->connectStart -=
                d->m_response.resourceLoadTiming()->dnsEnd - d->m_response.resourceLoadTiming()->dnsStart;
        break;
    case G_SOCKET_CLIENT_CONNECTED:
        // Web Timing considers that connection time involves dns, proxy & TLS negotiation...
        // so we better pick G_SOCKET_CLIENT_COMPLETE for connectEnd
        break;
    case G_SOCKET_CLIENT_PROXY_NEGOTIATING:
        d->m_response.resourceLoadTiming()->proxyStart = deltaTime;
        break;
    case G_SOCKET_CLIENT_PROXY_NEGOTIATED:
        d->m_response.resourceLoadTiming()->proxyEnd = deltaTime;
        break;
    case G_SOCKET_CLIENT_TLS_HANDSHAKING:
        d->m_response.resourceLoadTiming()->sslStart = deltaTime;
        break;
    case G_SOCKET_CLIENT_TLS_HANDSHAKED:
        d->m_response.resourceLoadTiming()->sslEnd = deltaTime;
        break;
    case G_SOCKET_CLIENT_COMPLETE:
        d->m_response.resourceLoadTiming()->connectEnd = deltaTime;
        break;
    default:
        ASSERT_NOT_REACHED();
        break;
    }
}
#endif

static const char* gSoupRequestInitiaingPageIDKey = "wk-soup-request-initiaing-page-id";

static void setSoupRequestInitiaingPageIDFromNetworkingContext(SoupRequest* request, NetworkingContext* context)
{
    if (!context || !context->isValid())
        return;

    uint64_t* initiatingPageIDPtr = static_cast<uint64_t*>(fastMalloc(sizeof(uint64_t)));
    *initiatingPageIDPtr = context->initiatingPageID();
    g_object_set_data_full(G_OBJECT(request), g_intern_static_string(gSoupRequestInitiaingPageIDKey), initiatingPageIDPtr, fastFree);
}

static bool createSoupMessageForHandleAndRequest(ResourceHandle* handle, const ResourceRequest& request)
{
    ASSERT(handle);

    ResourceHandleInternal* d = handle->getInternal();
    ASSERT(d->m_soupRequest);

    d->m_soupMessage = adoptGRef(soup_request_http_get_message(SOUP_REQUEST_HTTP(d->m_soupRequest.get())));
    if (!d->m_soupMessage)
        return false;

    SoupMessage* soupMessage = d->m_soupMessage.get();
    request.updateSoupMessage(soupMessage);

    g_object_set_data(G_OBJECT(soupMessage), "handle", handle);
    if (!handle->shouldContentSniff())
        soup_message_disable_feature(soupMessage, SOUP_TYPE_CONTENT_SNIFFER);

    String firstPartyString = request.firstPartyForCookies().string();
    if (!firstPartyString.isEmpty()) {
        GOwnPtr<SoupURI> firstParty(soup_uri_new(firstPartyString.utf8().data()));
        soup_message_set_first_party(soupMessage, firstParty.get());
    }

    FormData* httpBody = request.httpBody();
    CString contentType = request.httpContentType().utf8().data();
    if (httpBody && !httpBody->isEmpty() && !addFormElementsToSoupMessage(soupMessage, contentType.data(), httpBody, d->m_bodySize)) {
        // We failed to prepare the body data, so just fail this load.
        d->m_soupMessage.clear();
        return false;
    }

    // Make sure we have an Accept header for subresources; some sites
    // want this to serve some of their subresources
    if (!soup_message_headers_get_one(soupMessage->request_headers, "Accept"))
        soup_message_headers_append(soupMessage->request_headers, "Accept", "*/*");

    // In the case of XHR .send() and .send("") explicitly tell libsoup to send a zero content-lenght header
    // for consistency with other backends (e.g. Chromium's) and other UA implementations like FF. It's done
    // in the backend here instead of in XHR code since in XHR CORS checking prevents us from this kind of
    // late header manipulation.
    if ((request.httpMethod() == "POST" || request.httpMethod() == "PUT")
        && (!request.httpBody() || request.httpBody()->isEmpty()))
        soup_message_headers_set_content_length(soupMessage->request_headers, 0);

    g_signal_connect(d->m_soupMessage.get(), "got-headers", G_CALLBACK(gotHeadersCallback), handle);
    g_signal_connect(d->m_soupMessage.get(), "restarted", G_CALLBACK(restartedCallback), handle);
    g_signal_connect(d->m_soupMessage.get(), "wrote-body-data", G_CALLBACK(wroteBodyDataCallback), handle);

#if ENABLE(WEB_TIMING)
    d->m_response.setResourceLoadTiming(ResourceLoadTiming::create());
    g_signal_connect(d->m_soupMessage.get(), "network-event", G_CALLBACK(networkEventCallback), handle);
    g_signal_connect(d->m_soupMessage.get(), "wrote-body", G_CALLBACK(wroteBodyCallback), handle);
#endif

    return true;
}

static bool createSoupRequestAndMessageForHandle(ResourceHandle* handle, bool isHTTPFamilyRequest)
{
    ResourceHandleInternal* d = handle->getInternal();
    SoupRequester* requester = SOUP_REQUESTER(soup_session_get_feature(d->soupSession(), SOUP_TYPE_REQUESTER));

    GOwnPtr<GError> error;
    ResourceRequest& request = handle->firstRequest();

    d->m_soupRequest = adoptGRef(soup_requester_request(requester, request.urlStringForSoup().utf8().data(), &error.outPtr()));
    if (error) {
        d->m_soupRequest.clear();
        return false;
    }

    // SoupMessages are only applicable to HTTP-family requests.
    if (isHTTPFamilyRequest && !createSoupMessageForHandleAndRequest(handle, request)) {
        d->m_soupRequest.clear();
        return false;
    }

    return true;
}

bool ResourceHandle::start(NetworkingContext* context)
{
    ASSERT(!d->m_soupMessage);

    // The frame could be null if the ResourceHandle is not associated to any
    // Frame, e.g. if we are downloading a file.
    // If the frame is not null but the page is null this must be an attempted
    // load from an unload handler, so let's just block it.
    // If both the frame and the page are not null the context is valid.
    if (context && !context->isValid())
        return false;

    // Used to set the keep track of custom SoupSessions for ports that support it (EFL).
    d->m_context = context;

    // Only allow the POST and GET methods for non-HTTP requests.
    const ResourceRequest& request = firstRequest();
    bool isHTTPFamilyRequest = request.url().protocolIsInHTTPFamily();
    if (!isHTTPFamilyRequest && request.httpMethod() != "GET" && request.httpMethod() != "POST") {
        this->scheduleFailure(InvalidURLFailure); // Error must not be reported immediately
        return true;
    }

    applyAuthenticationToRequest(this, false);
    // The CFNet backend clears these, so we do as well.
    d->m_user = String();
    d->m_pass = String();

    if (!createSoupRequestAndMessageForHandle(this, isHTTPFamilyRequest)) {
        this->scheduleFailure(InvalidURLFailure); // Error must not be reported immediately
        return true;
    }

    setSoupRequestInitiaingPageIDFromNetworkingContext(d->m_soupRequest.get(), context);

    // Send the request only if it's not been explicitly deferred.
    if (!d->m_defersLoading)
        sendPendingRequest();

    return true;
}

void ResourceHandle::sendPendingRequest()
{
#if ENABLE(WEB_TIMING)
    if (d->m_response.resourceLoadTiming())
        d->m_response.resourceLoadTiming()->requestTime = monotonicallyIncreasingTime();
#endif

    if (d->m_firstRequest.timeoutInterval() > 0) {
        // soup_add_timeout returns a GSource* whose only reference is owned by
        // the context. We need to have our own reference to it, hence not using adoptRef.
        d->m_timeoutSource = soup_add_timeout(g_main_context_get_thread_default(),
            d->m_firstRequest.timeoutInterval() * 1000, requestTimeoutCallback, this);
    }

    // Balanced by a deref() in cleanupSoupRequestOperation, which should always run.
    ref();

    d->m_cancellable = adoptGRef(g_cancellable_new());
    soup_request_send_async(d->m_soupRequest.get(), d->m_cancellable.get(), sendRequestCallback, this);
}

void ResourceHandle::cancel()
{
    d->m_cancelled = true;
    if (d->m_soupMessage)
        soup_session_cancel_message(d->soupSession(), d->m_soupMessage.get(), SOUP_STATUS_CANCELLED);
    else if (d->m_cancellable)
        g_cancellable_cancel(d->m_cancellable.get());
}

bool ResourceHandle::shouldUseCredentialStorage()
{
    return (!client() || client()->shouldUseCredentialStorage(this)) && firstRequest().url().protocolIsInHTTPFamily();
}

void ResourceHandle::setHostAllowsAnyHTTPSCertificate(const String& host)
{
    allowsAnyHTTPSCertificateHosts().add(host.lower());
}

void ResourceHandle::setClientCertificate(const String& host, GTlsCertificate* certificate)
{
    clientCertificates().add(host.lower(), HostTLSCertificateSet()).iterator->value.add(certificate);
}

void ResourceHandle::setIgnoreSSLErrors(bool ignoreSSLErrors)
{
    gIgnoreSSLErrors = ignoreSSLErrors;
}

#if PLATFORM(GTK)
void getCredentialFromPersistentStoreCallback(const Credential& credential, void* data)
{
    static_cast<ResourceHandle*>(data)->continueDidReceiveAuthenticationChallenge(credential);
}
#endif

void ResourceHandle::continueDidReceiveAuthenticationChallenge(const Credential& credentialFromPersistentStorage)
{
    ASSERT(!d->m_currentWebChallenge.isNull());
    AuthenticationChallenge& challenge = d->m_currentWebChallenge;

    ASSERT(challenge.soupSession());
    ASSERT(challenge.soupMessage());
    if (!credentialFromPersistentStorage.isEmpty())
        challenge.setProposedCredential(credentialFromPersistentStorage);

    if (!client()) {
        soup_session_unpause_message(challenge.soupSession(), challenge.soupMessage());
        clearAuthentication();
        return;
    }

    ASSERT(challenge.soupSession());
    ASSERT(challenge.soupMessage());
    client()->didReceiveAuthenticationChallenge(this, challenge);
}

void ResourceHandle::didReceiveAuthenticationChallenge(const AuthenticationChallenge& challenge)
{
    ASSERT(d->m_currentWebChallenge.isNull());

    bool useCredentialStorage = shouldUseCredentialStorage();
    if (!d->m_user.isNull() && !d->m_pass.isNull()) {
        Credential credential = Credential(d->m_user, d->m_pass, CredentialPersistenceForSession);
        if (useCredentialStorage)
            CredentialStorage::set(credential, challenge.protectionSpace(), challenge.failureResponse().url());
        soup_auth_authenticate(challenge.soupAuth(), credential.user().utf8().data(), credential.password().utf8().data());

        return;
    }

    // FIXME: Per the specification, the user shouldn't be asked for credentials if there were incorrect ones provided explicitly.
    if (useCredentialStorage) {
        if (!d->m_initialCredential.isEmpty() || challenge.previousFailureCount()) {
            // The stored credential wasn't accepted, stop using it. There is a race condition
            // here, since a different credential might have already been stored by another
            // ResourceHandle, but the observable effect should be very minor, if any.
            CredentialStorage::remove(challenge.protectionSpace());
        }

        if (!challenge.previousFailureCount()) {
            Credential credential = CredentialStorage::get(challenge.protectionSpace());
            if (!credential.isEmpty() && credential != d->m_initialCredential) {
                ASSERT(credential.persistence() == CredentialPersistenceNone);

                // Store the credential back, possibly adding it as a default for this directory.
                if (challenge.failureResponse().httpStatusCode() == 401)
                    CredentialStorage::set(credential, challenge.protectionSpace(), challenge.failureResponse().url());

                soup_auth_authenticate(challenge.soupAuth(), credential.user().utf8().data(), credential.password().utf8().data());
                return;
            }
        }
    }

    d->m_currentWebChallenge = challenge;
    soup_session_pause_message(challenge.soupSession(), challenge.soupMessage());

#if PLATFORM(GTK)
    // We could also do this before we even start the request, but that would be at the expense
    // of all request latency, versus a one-time latency for the small subset of requests that
    // use HTTP authentication. In the end, this doesn't matter much, because persistent credentials
    // will become session credentials after the first use.
    if (useCredentialStorage) {
        credentialBackingStore().credentialForChallenge(challenge, getCredentialFromPersistentStoreCallback, this);
        return;
    }
#endif

    continueDidReceiveAuthenticationChallenge(Credential());
}

void ResourceHandle::receivedRequestToContinueWithoutCredential(const AuthenticationChallenge& challenge)
{
    ASSERT(!challenge.isNull());
    if (challenge != d->m_currentWebChallenge)
        return;
    soup_session_unpause_message(challenge.soupSession(), challenge.soupMessage());

    clearAuthentication();
}

void ResourceHandle::receivedCredential(const AuthenticationChallenge& challenge, const Credential& credential)
{
    ASSERT(!challenge.isNull());
    if (challenge != d->m_currentWebChallenge)
        return;

    // FIXME: Support empty credentials. Currently, an empty credential cannot be stored in WebCore credential storage, as that's empty value for its map.
    if (credential.isEmpty()) {
        receivedRequestToContinueWithoutCredential(challenge);
        return;
    }

    if (shouldUseCredentialStorage()) {
        // Eventually we will manage per-session credentials only internally or use some newly-exposed API from libsoup,
        // because once we authenticate via libsoup, there is no way to ignore it for a particular request. Right now,
        // we place the credentials in the store even though libsoup will never fire the authenticate signal again for
        // this protection space.
        if (credential.persistence() == CredentialPersistenceForSession || credential.persistence() == CredentialPersistencePermanent)
            CredentialStorage::set(credential, challenge.protectionSpace(), challenge.failureResponse().url());

#if PLATFORM(GTK)
        if (credential.persistence() == CredentialPersistencePermanent) {
            d->m_credentialDataToSaveInPersistentStore.credential = credential;
            d->m_credentialDataToSaveInPersistentStore.challenge = challenge;
        }
#endif
    }

    ASSERT(challenge.soupSession());
    ASSERT(challenge.soupMessage());
    soup_auth_authenticate(challenge.soupAuth(), credential.user().utf8().data(), credential.password().utf8().data());
    soup_session_unpause_message(challenge.soupSession(), challenge.soupMessage());

    clearAuthentication();
}

void ResourceHandle::receivedCancellation(const AuthenticationChallenge& challenge)
{
    ASSERT(!challenge.isNull());
    if (challenge != d->m_currentWebChallenge)
        return;

    soup_session_unpause_message(challenge.soupSession(), challenge.soupMessage());

    if (client())
        client()->receivedCancellation(this, challenge);

    clearAuthentication();
}

static bool waitingToSendRequest(ResourceHandle* handle)
{
    // We need to check for d->m_soupRequest because the request may have raised a failure
    // (for example invalid URLs). We cannot  simply check for d->m_scheduledFailure because
    // it's cleared as soon as the failure event is fired.
    return handle->getInternal()->m_soupRequest && !handle->getInternal()->m_cancellable;
}

void ResourceHandle::platformSetDefersLoading(bool defersLoading)
{
    if (d->m_cancelled)
        return;

    // Except when canceling a possible timeout timer, we only need to take action here to UN-defer loading.
    if (defersLoading) {
        if (d->m_timeoutSource) {
            g_source_destroy(d->m_timeoutSource.get());
            d->m_timeoutSource.clear();
        }
        return;
    }

    if (waitingToSendRequest(this)) {
        sendPendingRequest();
        return;
    }

    if (d->m_deferredResult) {
        GRefPtr<GAsyncResult> asyncResult = adoptGRef(d->m_deferredResult.leakRef());

        if (d->m_inputStream)
            readCallback(G_OBJECT(d->m_inputStream.get()), asyncResult.get(), this);
        else
            sendRequestCallback(G_OBJECT(d->m_soupRequest.get()), asyncResult.get(), this);
    }
}

bool ResourceHandle::loadsBlocked()
{
    return false;
}

bool ResourceHandle::willLoadFromCache(ResourceRequest&, Frame*)
{
    // Not having this function means that we'll ask the user about re-posting a form
    // even when we go back to a page that's still in the cache.
    notImplemented();
    return false;
}

void ResourceHandle::loadResourceSynchronously(NetworkingContext* context, const ResourceRequest& request, StoredCredentials /*storedCredentials*/, ResourceError& error, ResourceResponse& response, Vector<char>& data)
{
#if ENABLE(BLOB)
    if (request.url().protocolIs("blob")) {
        blobRegistry().loadResourceSynchronously(request, error, response, data);
        return;
    }
#endif

    ASSERT(!loadingSynchronousRequest);
    if (loadingSynchronousRequest) // In practice this cannot happen, but if for some reason it does,
        return;                    // we want to avoid accidentally going into an infinite loop of requests.

    WebCoreSynchronousLoader syncLoader(error, response, sessionFromContext(context), data);
    RefPtr<ResourceHandle> handle = create(context, request, &syncLoader, false /*defersLoading*/, false /*shouldContentSniff*/);
    if (!handle)
        return;

    // If the request has already failed, do not run the main loop, or else we'll block indefinitely.
    if (handle->d->m_scheduledFailureType != NoFailure)
        return;

    syncLoader.run();
}

static void closeCallback(GObject*, GAsyncResult* res, gpointer data)
{
    RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
    ResourceHandleInternal* d = handle->getInternal();

    g_input_stream_close_finish(d->m_inputStream.get(), res, 0);

    ResourceHandleClient* client = handle->client();
    if (client && loadingSynchronousRequest)
        client->didFinishLoading(handle.get(), 0);

    cleanupSoupRequestOperation(handle.get());
}

static void readCallback(GObject*, GAsyncResult* asyncResult, gpointer data)
{
    RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);

    ResourceHandleInternal* d = handle->getInternal();
    ResourceHandleClient* client = handle->client();

    if (d->m_cancelled || !client) {
        cleanupSoupRequestOperation(handle.get());
        return;
    }

    if (d->m_defersLoading) {
        d->m_deferredResult = asyncResult;
        return;
    }

    GOwnPtr<GError> error;
    gssize bytesRead = g_input_stream_read_finish(d->m_inputStream.get(), asyncResult, &error.outPtr());
    if (error) {
        client->didFail(handle.get(), ResourceError::genericIOError(error.get(), d->m_soupRequest.get()));
        cleanupSoupRequestOperation(handle.get());
        return;
    }

    if (!bytesRead) {
        // We inform WebCore of load completion now instead of waiting for the input
        // stream to close because the input stream is closed asynchronously. If this
        // is a synchronous request, we wait until the closeCallback, because we don't
        // want to halt the internal main loop before the input stream closes.
        if (client && !loadingSynchronousRequest) {
            client->didFinishLoading(handle.get(), 0);
            handle->setClient(0); // Unset the client so that we do not try to access th
                                  // client in the closeCallback.
        }
        g_input_stream_close_async(d->m_inputStream.get(), G_PRIORITY_DEFAULT, 0, closeCallback, handle.get());
        return;
    }

    // It's mandatory to have sent a response before sending data
    ASSERT(!d->m_response.isNull());

    client->didReceiveData(handle.get(), d->m_buffer, bytesRead, bytesRead);

    // didReceiveData may cancel the load, which may release the last reference.
    if (d->m_cancelled || !client) {
        cleanupSoupRequestOperation(handle.get());
        return;
    }

    g_input_stream_read_async(d->m_inputStream.get(), d->m_buffer, READ_BUFFER_SIZE, G_PRIORITY_DEFAULT,
                              d->m_cancellable.get(), readCallback, handle.get());
}

static gboolean requestTimeoutCallback(gpointer data)
{
    RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
    handle->client()->didFail(handle.get(), ResourceError::timeoutError(handle->getInternal()->m_firstRequest.url().string()));
    handle->cancel();

    return FALSE;
}

static void authenticateCallback(SoupSession* session, SoupMessage* soupMessage, SoupAuth* soupAuth, gboolean retrying)
{
    RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(g_object_get_data(G_OBJECT(soupMessage), "handle"));
    if (!handle)
        return;
    handle->didReceiveAuthenticationChallenge(AuthenticationChallenge(session, soupMessage, soupAuth, retrying, handle.get()));
}

SoupSession* ResourceHandle::defaultSession()
{
    static SoupSession* session = 0;
    // Values taken from http://www.browserscope.org/  following
    // the rule "Do What Every Other Modern Browser Is Doing". They seem
    // to significantly improve page loading time compared to soup's
    // default values.
    static const int maxConnections = 35;
    static const int maxConnectionsPerHost = 6;

    if (!session) {
        session = soup_session_async_new();
        g_object_set(session,
                     SOUP_SESSION_MAX_CONNS, maxConnections,
                     SOUP_SESSION_MAX_CONNS_PER_HOST, maxConnectionsPerHost,
                     SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_CONTENT_DECODER,
                     SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_CONTENT_SNIFFER,
                     SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_PROXY_RESOLVER_DEFAULT,
                     SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
                     NULL);
        g_signal_connect(session, "authenticate", G_CALLBACK(authenticateCallback), 0);

#if ENABLE(WEB_TIMING)
        g_signal_connect(session, "request-started", G_CALLBACK(requestStartedCallback), 0);
#endif
    }

    return session;
}

uint64_t ResourceHandle::getSoupRequestInitiaingPageID(SoupRequest* request)
{
    uint64_t* initiatingPageIDPtr = static_cast<uint64_t*>(g_object_get_data(G_OBJECT(request), gSoupRequestInitiaingPageIDKey));
    return initiatingPageIDPtr ? *initiatingPageIDPtr : 0;
}

}

redpajama_set_name: RedPajamaGithub


text:
//  Copyright (C) 2014 Federico Recio
/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.federecio.dropwizard.swagger.selenium.auth;

import io.dropwizard.auth.AuthDynamicFeature;
import io.dropwizard.auth.AuthValueFactoryProvider;
import io.dropwizard.auth.PrincipalImpl;
import io.dropwizard.auth.oauth.OAuthCredentialAuthFilter;
import io.dropwizard.setup.Environment;
import io.federecio.dropwizard.swagger.TestApplication;
import io.federecio.dropwizard.swagger.TestConfiguration;

public class TestAuthApplication extends TestApplication {

    @Override
    public void run(TestConfiguration configuration, Environment environment) throws Exception {
        super.run(configuration, environment);
        environment.jersey().register(new AuthResource());

        environment.jersey().register(new AuthDynamicFeature(
                new OAuthCredentialAuthFilter.Builder<PrincipalImpl>()
                    .setAuthenticator(new TestAuthenticator())
                    .setRealm("SUPER SECRET STUFF")
                    .setPrefix("Bearer")
                    .buildAuthFilter()));
        environment.jersey().register(new AuthValueFactoryProvider.Binder<>(PrincipalImpl.class));
    }
}


text:
package com.cloudbees.plugins.credentials.common;

import com.cloudbees.plugins.credentials.Credentials;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import org.apache.commons.lang.StringUtils;

import java.util.UUID;

/**
 * Credentials that have an unique ID that assists in retrieving the specific credential from a collection of
 * {@link Credentials}.
 * <p/>
 * Note: This credential interface is a marker interface
 *
 * @since 1.5
 */
@LegacyMixIn(preferred = StandardCredentials.class)
public interface IdCredentials extends Credentials {
    /**
     * Returns the ID.
     *
     * @return the ID.
     */
    @NonNull
    String getId();

    /**
     * The contract implementations of {@link #equals(Object)} and {@link #hashCode()} that implementations of
     * this class must use as the basis for equality tests and as the hash code.
     *
     * @since 1.6
     */
    public static final class Helpers {
        /**
         * Standard {@link #equals(Object)} implementation.
         *
         * @param self the {@code this} reference.
         * @param o    the other object.
         * @return true if equal.
         */
        public static boolean equals(IdCredentials self, Object o) {
            if (self == o) {
                return true;
            }
            if (!(o instanceof IdCredentials)) {
                return false;
            }

            IdCredentials that = (IdCredentials) o;

            if (!self.getId().equals(that.getId())) {
                return false;
            }

            return true;
        }

        /**
         * The standard {@link #hashCode()} implementation.
         *
         * @param self the {@code this} reference.
         * @return the hash code.
         */
        public static int hashCode(IdCredentials self) {
            return self.getId().hashCode();
        }

        /**
         * Returns either the id or a generated new id if the supplied id is missing.
         *
         * @param id the supplied id.
         * @return either the id or a generated new id if the supplied id is missing.
         */
        @NonNull
        public static String fixEmptyId(@CheckForNull String id) {
            return StringUtils.isEmpty(id) ? UUID.randomUUID().toString() : id;
        }
    }
}


text:
module Stratocumulus
  class Database
    # #rewind is undefined so fog won't try to call rewind on a pipe
    class PipeIO < IO
      undef rewind
    end
  end
end


text:
/**
 * A pure angular version of the cloudinary_angular plugin including a few new directives not found in the official library.
 *
 * Based on cloudinary_js version 1.0.25 and cloudinary_angular version 0.1.4
 */
declare namespace cloudinary {
    interface Service {
        configure: Function;
        url: Function;
        video_url: Function;
        video_thumbnail_url: Function;
    }
    interface Config {
        cloud_name: string;
        api_key: string;
        private_cdn?: string;
        secure_distribution?: string;
        cname?: string;
        cdn_subdomain?: string;
        secure_cdn_subdomain?: string;
        shorten?: boolean;
        protocol?: string;
        use_root_path?: boolean;
    }
}


text:
package org.goatlovers.goats.client.characters;

import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.goatlovers.goats.core.Action;

public class Brain {

    private static final String DEFAULT_AI_SCRIPT = "../ai/simple-ai.groovy";

    private static final String AI_METHOD = "think";

    private GroovyObject aiObject = null;

    public Brain() {
        loadAI(new File(DEFAULT_AI_SCRIPT));
    }

    @SuppressWarnings("unchecked")
    public List<Action> think() {
        Object[] args = {};
        return (List<Action>) aiObject.invokeMethod(AI_METHOD, args);
    }

    @SuppressWarnings("rawtypes")
    private void loadAI(File aiScript) {
        GroovyClassLoader loader = null;
        try {
            ClassLoader parent = getClass().getClassLoader();
            loader = new GroovyClassLoader(parent);
            Class groovyClass = loader.parseClass(new File(DEFAULT_AI_SCRIPT));
            aiObject = (GroovyObject) groovyClass.newInstance();
            loader.close();
        } catch (IOException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        } finally {
            if (loader != null) {
                try {
                    loader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


text:
from PyQt5 import Qt
from .flipbook import ImageList

class _BaseField(Qt.QObject):
    widget_value_changed = Qt.pyqtSignal(object)

    def __init__(self, field_tuple, parent):
        super().__init__(parent)
        self.name = field_tuple[0]
        self.type = field_tuple[1]
        self.default = field_tuple[2]
        self.field_tuple = field_tuple
        self._init_widget()

    def _init_widget(self):
        pass

    def _on_widget_change(self):
        self.widget_value_changed.emit(self)

    def refresh(self, value):
        self.widget.setValue(value)

    def value(self):
        return self.widget.value()

class _NoClickToPartialTristateCheckbox(Qt.QCheckBox):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setTristate(True)

    def nextCheckState(self):
        # Do not enter partially clicked state in response to checkbox widget manipulation.  Calling
        # setCheckState(Qt.Qt.PartiallyChecked) is not affected (that is, continues to work as normal).
        self.setCheckState(Qt.Qt.Unchecked if self.isChecked() else Qt.Qt.Checked)

class _BoolField(_BaseField):
    CHECK_STATE_VALUES = {
        Qt.Qt.Checked : True,
        Qt.Qt.Unchecked : False,
        Qt.Qt.PartiallyChecked : None
    }
    VALUE_CHECK_STATES = {v : k for k, v in CHECK_STATE_VALUES.items()}

    def _init_widget(self):
        self.widget = _NoClickToPartialTristateCheckbox()
        self.widget.stateChanged.connect(self._on_widget_change)
        self.widget.setFocusPolicy(Qt.Qt.WheelFocus)

    def refresh(self, value):
        self.widget.setCheckState(self.VALUE_CHECK_STATES[value])

    def value(self):
        return self.CHECK_STATE_VALUES[self.widget.checkState()]

class _StringField(_BaseField):
    def _init_widget(self):
        self.widget = Qt.QLineEdit()
        self.widget.textEdited.connect(self._on_widget_change)

    def refresh(self, value):
        self.widget.setText(value)

    def value(self):
        return self.widget.text()

class _NumField(_BaseField):
    def _init_widget(self):
        self.widget = Qt.QLineEdit()
        self.min = self.field_tuple[3] if len(self.field_tuple) >= 4 else None
        self.max = self.field_tuple[4] if len(self.field_tuple) >= 5 else None
        self.widget.textEdited.connect(self._on_widget_change)
        self.widget.editingFinished.connect(self._on_editing_finished)

    def _on_editing_finished(self):
        v = self.value()
        self.refresh(v)

    def refresh(self, value):
        self.widget.setText('' if value is None else str(value))

    def value(self):
        t = self.widget.text()
        if len(t) == 0:
            return None
        try:
            v = self.NUM_TYPE(t)
        except ValueError:
            return self.default
        if self.min is not None and v < self.min:
            return self.min
        elif self.max is not None and v > self.max:
            return self.max
        else:
            return v

class _IntField(_NumField):
    NUM_TYPE = int

class _FloatField(_NumField):
    NUM_TYPE = float

class _ChoiceField(_BaseField):
    def __init__(self, field_tuple, parent):
        choices = tuple(field_tuple[3])
        assert len(set(choices)) == len(choices), "choices must be unique"
        assert field_tuple[2] in choices, "value supplied for default must be a choice"
        self.choices = choices
        super().__init__(field_tuple, parent)

    def _init_widget(self):
        self.widget = Qt.QComboBox()
        for choice in self.choices:
            self.widget.addItem(choice)
        self.widget.currentIndexChanged[str].connect(self._on_widget_change)
        self.widget.setFocusPolicy(Qt.Qt.WheelFocus)

    def refresh(self, value):
        if value is None:
            self.widget.setCurrentIndex(-1)
        else:
            self.widget.setCurrentText(value)

    def value(self):
        i = self.widget.currentIndex()
        return None if i==-1 else self.choices[i]

class FlipbookPageAnnotator(Qt.QWidget):
    """Field widgets are grayed out when no flipbook entry is selected."""
    TYPE_FIELD_CLASSES = {
        bool: _BoolField,
        str: _StringField,
        int: _IntField,
        float : _FloatField,
        tuple : _ChoiceField
    }
    def __init__(self, flipbook, page_metadata_attribute_name, field_descrs, parent=None):
        super().__init__(parent)
        self.setAttribute(Qt.Qt.WA_DeleteOnClose)
        self.flipbook = flipbook
        flipbook.page_selection_changed.connect(self._on_page_selection_changed)
        self.page_metadata_attribute_name = page_metadata_attribute_name
        layout = Qt.QFormLayout()
        layout.setFieldGrowthPolicy(layout.ExpandingFieldsGrow)
        self.setLayout(layout)
        self.fields = {}
        self._ignore_gui_change = False
        for field_descr in field_descrs:
            assert field_descr[0] not in self.fields
            field = self._make_field(field_descr)
            field.refresh(field_descr[2])
            field.widget_value_changed.connect(self._on_gui_change)
            layout.addRow(field.name, field.widget)
            self.fields[field_descr[0]] = field
        self.refresh_gui()

    @property
    def data(self):
        data = []
        for page in self.flipbook.pages:
            if hasattr(page, self.page_metadata_attribute_name):
                page_data = getattr(page, self.page_metadata_attribute_name)
            else:
                page_data = {}
                setattr(page, self.page_metadata_attribute_name, page_data)
            for field in self.fields.values():
                if field.name not in page_data:
                    page_data[field.name] = field.default
            data.append(page_data)
        return data

    @data.setter
    def data(self, v):
        # Replace relevant values in annotations of corresponding pages.  In the situation where an incomplete
        # dict is supplied for a page also missing the omitted values, defaults are assigned.
        m_n = self.page_metadata_attribute_name
        for page_v, page in zip(v, self.flipbook.pages):
            old_page_data = getattr(page, m_n, {})
            updated_page_data = {}
            for field in self.fields.values():
                n = field.name
                if n in page_v:
                    updated_page_data[n] = page_v[n]
                elif n in old_page_data:
                    updated_page_data[n] = old_page_data[n]
                else:
                    updated_page_data[n] = field.default
            setattr(page, m_n, updated_page_data)
        self.refresh_gui()

    def _make_field(self, field_descr):
        return self.TYPE_FIELD_CLASSES[field_descr[1]](field_descr, self)

    def _on_page_selection_changed(self):
        self.refresh_gui()

    def _on_gui_change(self, field):
        if not self._ignore_gui_change:
            pages = self.flipbook.selected_pages
            m_n = self.page_metadata_attribute_name
            v = field.value()
            if v is None:
                return
            for page in pages:
                try:
                    data = getattr(page, m_n)
                except AttributeError:
                    continue
                data[field.name] = v

    def refresh_gui(self):
        """Ensures that the currently selected flipbook pages' annotation dicts contain at least default values, and
        updates the annotator GUI with data from the annotation dicts."""
        pages = self.flipbook.selected_pages
        layout = self.layout()
        self._ignore_gui_change = True
        try:
            if len(pages) == 0:
                for field in self.fields.values():
                    field.widget.setEnabled(False)
                    layout.labelForField(field.widget).setEnabled(False)
            elif len(pages) == 1:
                page = pages[0]
                if hasattr(page, self.page_metadata_attribute_name):
                    data = getattr(page, self.page_metadata_attribute_name)
                else:
                    data = {}
                    setattr(page, self.page_metadata_attribute_name, data)
                for field in self.fields.values():
                    if field.name in data:
                        v = data[field.name]
                    else:
                        v = data[field.name] = field.default
                    field.refresh(v)
                    field.widget.setEnabled(True)
                    layout.labelForField(field.widget).setEnabled(True)
            else:
                initial = True
                for page in pages:
                    if not isinstance(page, ImageList):
                        continue
                    if hasattr(page, self.page_metadata_attribute_name):
                        data = getattr(page, self.page_metadata_attribute_name)
                    else:
                        data = {}
                        setattr(page, self.page_metadata_attribute_name, data)
                    for field in self.fields.values():
                        if field.name not in data:
                            data[field.name] = field.default
                    if initial:
                        initial = False
                        fvs = {}
                        for field in self.fields.values():
                            v = fvs[field.name] = data[field.name]
                            field.widget.setEnabled(True)
                    else:
                        for field_name, field_value in list(fvs.items()):
                            if data[field_name] != field_value:
                                del fvs[field_name]
                for field in self.fields.values():
                    field.refresh(fvs.get(field.name))
        finally:
            self._ignore_gui_change = False


text:
/**
 * @namespace JARS~internals.Configs.Hooks
 */
JARS.internalGroup('Configs/Hooks', [
    'Debugging',
    'Environment',
    'Environments',
    'GlobalAccess',
    'Interceptors',
    'Main',
    'Modules'
]);


text:
<?xml version="1.0" encoding="UTF-8"?>
<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>distribution</id>
    <formats>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <outputDirectory>/</outputDirectory>
            <directory>${project.build.directory}/jsondoc-ui</directory>
            <filtered>true</filtered>
            <excludes>
                <exclude>**/META-INF/**</exclude>
            </excludes>
        </fileSet>
    </fileSets>
    <includeBaseDirectory>false</includeBaseDirectory>
</assembly>


text:
@implementation SoundPreferencesViewController

@synthesize soundInputPopUp = soundInputPopUp_;
@synthesize soundOutputPopUp = soundOutputPopUp_;
@synthesize ringtoneOutputPopUp = ringtoneOutputPopUp_;
@synthesize ringtonePopUp = ringtonePopUp_;

- (id)init {
  self = [super initWithNibName:@"SoundPreferencesView" bundle:nil];
  if (self != nil) {
    [self setTitle:NSLocalizedString(@"Sound",
                                     @"Sound preferences window title.")];
  }

  return self;
}

- (void)awakeFromNib {
  NSNotificationCenter *notificationCenter
    = [NSNotificationCenter defaultCenter];

  // Subscribe on mouse-down event of the ringing sound selection.
  [notificationCenter addObserver:self
                         selector:@selector(popUpButtonWillPopUp:)
                             name:NSPopUpButtonWillPopUpNotification
                           object:[self ringtonePopUp]];

  [self updateAvailableSounds];
  [self updateAudioDevices];
}

- (void)dealloc {
  [soundInputPopUp_ release];
  [soundOutputPopUp_ release];
  [ringtoneOutputPopUp_ release];
  [ringtonePopUp_ release];

  [[NSNotificationCenter defaultCenter] removeObserver:self];

  [super dealloc];
}

- (IBAction)changeSoundIO:(id)sender {
  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  [defaults setObject:[[self soundInputPopUp] titleOfSelectedItem]
               forKey:kSoundInput];
  [defaults setObject:[[self soundOutputPopUp] titleOfSelectedItem]
               forKey:kSoundOutput];
  [defaults setObject:[[self ringtoneOutputPopUp] titleOfSelectedItem]
               forKey:kRingtoneOutput];

  [[NSApp delegate] selectSoundIO];
}

- (void)updateAudioDevices {
  // Populate sound IO pop-up buttons.
  NSArray *audioDevices = [[NSApp delegate] audioDevices];
  NSMenu *soundInputMenu = [[[NSMenu alloc] init] autorelease];
  NSMenu *soundOutputMenu = [[[NSMenu alloc] init] autorelease];
  NSMenu *ringtoneOutputMenu = [[[NSMenu alloc] init] autorelease];

  for (NSUInteger i = 0; i < [audioDevices count]; ++i) {
    NSDictionary *deviceDict = [audioDevices objectAtIndex:i];

    NSMenuItem *aMenuItem = [[NSMenuItem alloc] init];
    [aMenuItem setTitle:[deviceDict objectForKey:kAudioDeviceName]];
    [aMenuItem setTag:i];

    if ([[deviceDict objectForKey:kAudioDeviceInputsCount] integerValue] > 0)
      [soundInputMenu addItem:[[aMenuItem copy] autorelease]];

    if ([[deviceDict objectForKey:kAudioDeviceOutputsCount] integerValue] > 0) {
      [soundOutputMenu addItem:[[aMenuItem copy] autorelease]];
      [ringtoneOutputMenu addItem:[[aMenuItem copy] autorelease]];
    }

    [aMenuItem release];
  }

  [[self soundInputPopUp] setMenu:soundInputMenu];
  [[self soundOutputPopUp] setMenu:soundOutputMenu];
  [[self ringtoneOutputPopUp] setMenu:ringtoneOutputMenu];

  // Select saved sound devices.

  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

  NSString *lastSoundInput = [defaults stringForKey:kSoundInput];
  if (lastSoundInput != nil &&
      [[self soundInputPopUp] itemWithTitle:lastSoundInput] != nil) {
    [[self soundInputPopUp] selectItemWithTitle:lastSoundInput];
  }

  NSString *lastSoundOutput = [defaults stringForKey:kSoundOutput];
  if (lastSoundOutput != nil &&
      [[self soundOutputPopUp] itemWithTitle:lastSoundOutput] != nil) {
    [[self soundOutputPopUp] selectItemWithTitle:lastSoundOutput];
  }

  NSString *lastRingtoneOutput = [defaults stringForKey:kRingtoneOutput];
  if (lastRingtoneOutput != nil &&
      [[self ringtoneOutputPopUp] itemWithTitle:lastRingtoneOutput] != nil) {
    [[self ringtoneOutputPopUp] selectItemWithTitle:lastRingtoneOutput];
  }
}

- (void)updateAvailableSounds {
  NSArray *libraryPaths
    = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
                                          NSAllDomainsMask,
                                          YES);
  if ([libraryPaths count] <= 0)
    return;

  NSMenu *soundsMenu = [[[NSMenu alloc] init] autorelease];
  NSFileManager *fileManager = [NSFileManager defaultManager];
  NSSet *allowedSoundFileExtensions
    = [NSSet setWithObjects:@"aiff", @"aif", @"aifc",
       @"mp3", @"wav", @"sd2", @"au", @"snd", @"m4a", @"m4p", nil];

  for (NSUInteger i = 0; i < [libraryPaths count]; ++i) {
    NSString *aPath = [libraryPaths objectAtIndex:i];
    NSString *soundPath = [aPath stringByAppendingPathComponent:@"Sounds"];
    NSArray *soundFiles = [fileManager contentsOfDirectoryAtPath:soundPath
                                                           error:NULL];

    BOOL shouldAddSeparator = ([soundsMenu numberOfItems] > 0) ? YES : NO;

    for (NSUInteger j = 0; j < [soundFiles count]; ++j) {
      NSString *aFile = [soundFiles objectAtIndex:j];
      if (![allowedSoundFileExtensions containsObject:[aFile pathExtension]])
        continue;

      NSString *aSound = [aFile stringByDeletingPathExtension];
      if ([soundsMenu itemWithTitle:aSound] == nil) {
        if (shouldAddSeparator) {
          [soundsMenu addItem:[NSMenuItem separatorItem]];
          shouldAddSeparator = NO;
        }

        NSMenuItem *aMenuItem = [[[NSMenuItem alloc] init] autorelease];
        [aMenuItem setTitle:aSound];
        [soundsMenu addItem:aMenuItem];
      }
    }
  }

  [[self ringtonePopUp] setMenu:soundsMenu];

  NSString *savedSound
    = [[NSUserDefaults standardUserDefaults] stringForKey:kRingingSound];

  if ([soundsMenu itemWithTitle:savedSound] != nil)
    [[self ringtonePopUp] selectItemWithTitle:savedSound];
}

- (IBAction)changeRingtone:(id)sender {
  [[[NSApp delegate] ringtone] stop];

  NSString *soundName = [sender title];
  [[NSUserDefaults standardUserDefaults] setObject:soundName
                                            forKey:kRingingSound];
  [[NSApp delegate] setRingtone:[NSSound soundNamed:soundName]];

  // Play selected ringtone once.
  [[[NSApp delegate] ringtone] play];
}

#pragma mark -
#pragma mark NSPopUpButton notification

- (void)popUpButtonWillPopUp:(NSNotification *)notification {
  [self updateAvailableSounds];
}

@end


text:
gx:
	go get github.com/whyrusleeping/gx
	go get github.com/whyrusleeping/gx-go

deps: gx
	gx --verbose install --global
	gx-go rewrite

publish:
	gx-go rewrite --undo


text:
package me.thehutch.fusion.api.event;

/**
 * @author thehutch
 */
public enum EventPriority {
	/**
	 * High priority event, called before all other priorities.
	 */
	HIGH,
	/**
	 * Medium priority event, called after {@link #HIGH} priority.
	 */
	MEDIUM,
	/**
	 * Low priority event, called after {@link #MEDIUM} priority.
	 */
	LOW;
	private static final EventPriority[] VALUES = EventPriority.values();

	/**
	 * Default constructor for {@link EventPriority}.
	 *
	 * @param priority The level of priority
	 */
	private EventPriority() {
	}

	/**
	 * Returns the {@link EventPriority} at the given index based on their ordinal.
	 *
	 * @param index The index of the priority
	 *
	 * @return The {@link EventPriority} at the given index
	 */
	public static EventPriority getByIndex(int index) {
		if (index < 0 || index >= VALUES.length) {
			throw new ArrayIndexOutOfBoundsException(index);
		}
		return VALUES[index];
	}
}


text:
=======
Credits
=======

Development Lead
----------------

* Ralph Brecheisen <ralph.brecheisen@gmail.com>

Contributors
------------

None yet. Why not be the first?


text:
DATA localizedData
{
    # same as culture = "en-US"
    ConvertFrom-StringData @'
'@
}

if (Test-Path $PSScriptRoot\en-us)
{
    $ModuleName = Split-Path $PSScriptRoot -Leaf
    Import-LocalizedData LocalizedData -filename "$($ModuleName)Provider.psd1"
}

function Get-TargetResource
{
    [OutputType([Hashtable])]
    param (
        [parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Name,
        [ValidateSet('Present','Absent')]
        [string]
        $Ensure = 'Present'
    )

    #Needs to return a hashtable that returns the current
    #status of the configuration component
    $Configuration = @{
        Name = $Name
    }
    return $Configuration
}

function Set-TargetResource
{
    param (
        [parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Name,
        [parameter()]
        [ValidateSet('Present','Absent')]
        [string]
        $Ensure = 'Present'
    )

}

function Test-TargetResource
{
    [OutputType([boolean])]
    param (
        [parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Name,
        [parameter()]
        [ValidateSet('Present','Absent')]
        [string]
        $Ensure = 'Present'
    )

    #Needs to return a boolean
    return $true
}


text:
<!DOCTYPE html>

    <meta http-equiv="refresh" content="0;URL=../../../ioctl_rs/constant.TIOCNXCL.html">

Redirecting to ../../../ioctl_rs/constant.TIOCNXCL.html...


text:
shows.43n79w.com
================

A proof-of-concept AngularJS app that lists concerts around Toronto.

shows.43n79w.com is licensed under the Apache License, Version 2.0
(http://www.apache.org/licenses/LICENSE-2.0.html).


text:
#include <zephyr.h>
#include <device.h>
#include <gpio.h>
#include <misc/printk.h>
#include <misc/__assert.h>
#include <board.h>
#include <string.h>

/* size of stack area used by each thread */
#define STACKSIZE 1024

/* scheduling priority used by each thread */
#define PRIORITY 7

/* Change this if you have an LED connected to a custom port */
#ifndef LED0_GPIO_CONTROLLER
#define LED0_GPIO_CONTROLLER 	LED0_GPIO_PORT
#endif
#ifndef LED1_GPIO_CONTROLLER
#define LED1_GPIO_CONTROLLER 	LED1_GPIO_PORT
#endif

#define PORT0	 LED0_GPIO_CONTROLLER
#define PORT1	 LED1_GPIO_CONTROLLER

/* Change this if you have an LED connected to a custom pin */
#define LED0    LED0_GPIO_PIN
#define LED1    LED1_GPIO_PIN

struct printk_data_t {
	void *fifo_reserved; /* 1st word reserved for use by fifo */
	u32_t led;
	u32_t cnt;
};

K_FIFO_DEFINE(printk_fifo);

void blink(const char *port, u32_t sleep_ms, u32_t led, u32_t id)
{
	int cnt = 0;
	struct device *gpio_dev;

	gpio_dev = device_get_binding(port);
	__ASSERT_NO_MSG(gpio_dev != NULL);

	gpio_pin_configure(gpio_dev, led, GPIO_DIR_OUT);

	while (1) {
		gpio_pin_write(gpio_dev, led, cnt % 2);

		struct printk_data_t tx_data = { .led = id, .cnt = cnt };

		size_t size = sizeof(struct printk_data_t);
		char *mem_ptr = k_malloc(size);
		__ASSERT_NO_MSG(mem_ptr != 0);

		memcpy(mem_ptr, &tx_data, size);

		k_fifo_put(&printk_fifo, mem_ptr);

		k_sleep(sleep_ms);
		cnt++;
	}
}

void blink1(void)
{
	blink(PORT0, 100, LED0, 0);
}

void blink2(void)
{
	blink(PORT1, 1000, LED1, 1);
}

void uart_out(void)
{
	while (1) {
		struct printk_data_t *rx_data = k_fifo_get(&printk_fifo, K_FOREVER);
		printk("Toggle USR%d LED: Counter = %d\n", rx_data->led, rx_data->cnt);
		k_free(rx_data);
	}
}

K_THREAD_DEFINE(blink1_id, STACKSIZE, blink1, NULL, NULL, NULL,
		PRIORITY, 0, K_NO_WAIT);
K_THREAD_DEFINE(blink2_id, STACKSIZE, blink2, NULL, NULL, NULL,
		PRIORITY, 0, K_NO_WAIT);
K_THREAD_DEFINE(uart_out_id, STACKSIZE, uart_out, NULL, NULL, NULL,
		PRIORITY, 0, K_NO_WAIT);


text:
import moment from 'moment';
import Config from '/server/hardConfig.js';

export function sc2mn(seconds) {
  const asMinutes = moment.duration(seconds, "seconds").asMinutes();
  const trunc = ( Math.round(
                   (asMinutes + Number.EPSILON) * 100)
                      / 100 ).toFixed(2, 10);
  return parseFloat(trunc);
}

export function sc2hr(seconds) {
  const asHours = moment.duration(seconds, "seconds").asHours();
  const trunc = ( Math.round(
                   (asHours + Number.EPSILON) * 100)
                      / 100 ).toFixed(2, 10);
  return parseFloat(trunc);
}

export function min2hr(minutes) {
  const asHours = moment.duration(minutes, "minutes").asHours();
  const trunc = ( Math.round(
                   (asHours + Number.EPSILON) * 100)
                      / 100 ).toFixed(2, 10);
  return parseFloat(trunc);
}

export function round1Decimal(thrtytw) {
  const trunc = Math.round((thrtytw + Number.EPSILON) * 10) / 10;
  return trunc;
}

export function round2Decimal(thrtytw) {
  const trunc = Math.round((thrtytw + Number.EPSILON) * 100) / 100;
  return trunc;
}

export function percentOf(goalNumber, realNumber) {
  const perOf = ( realNumber / goalNumber ) * 100;
  const perOfClean = isNaN(perOf) ? 0 : round2Decimal(perOf);
  return perOfClean;
}

export function percentOverUnder(goalNumber, realNumber) {
  const overunder = ( 1 - ( realNumber / goalNumber ) ) * 100;
  const overunderClean = isNaN(overunder) ? 0 : round2Decimal(overunder);
  return overunderClean;
}

export function avgOfArray(arr, zeros) {
  const cArr = zeros ? arr.filter( f => ( f || f === 0 ) && isFinite(f) ) :
                       arr.filter( f => f && isFinite(f) );

  if(cArr.length == 1) {
    return round2Decimal( cArr[0] );
  }else if(cArr.length > 1) {
    const reduced = cArr.reduce( (a,c)=> a + c ) / cArr.length;
    const clean = round2Decimal( reduced );
    return clean;
  }else{
    return 0;
  }
}

export function avg4est(mQuote, mEst) {
  if(!mQuote && !mEst) {
    return 0;
  }else if(!mQuote) {
    return round2Decimal( mEst );
  }else if(!mEst) {
    return round2Decimal( mQuote );
  }else{
    const lAvg = ( ( mEst * Config.estLean ) + mQuote ) / ( 1 + Config.estLean );
    return round2Decimal( lAvg );
  }
}

export function asRate(instances, ofQty, one) {
  const divide = instances / ofQty;
  const finite = isFinite(divide) ? divide : 0;
  if(one) {
    return round1Decimal( finite );
  }else{
    return round2Decimal( finite );
  }
}

export function diffTrend(newavg, runningavg) {
  const diff = Number(newavg) - Number(runningavg);
  const trend = Math.abs(diff) <= (runningavg * 0.03) ? 'flat' :
                diff < 0 ? 'down' : 'up';
  return trend;
}

// Quadratic Regression Equation
export function quadRegression(x) {
  // x = %_of_completed
  // y = %_of_time_used
  const y = Config.qregA + (Config.qregB * x ) + ( Config.qregC * Math.pow(x, 2) );
  const v = isNaN(y) ? 0 : round2Decimal(y);
  return v;
}


text:
#include "SDL_config.h"

#ifndef _SDL_x11opengl_h
#define _SDL_x11opengl_h

#if SDL_VIDEO_OPENGL_GLX
#include "SDL_opengl.h"
#include <GL/glx.h>

struct SDL_GLDriverData
{
    SDL_bool HAS_GLX_EXT_visual_rating;

    void *(*glXGetProcAddress) (const GLubyte * procName);

    XVisualInfo *(*glXChooseVisual)
      (Display * dpy, int screen, int *attribList);

      GLXContext(*glXCreateContext)
      (Display * dpy, XVisualInfo * vis, GLXContext shareList, Bool direct);

    void (*glXDestroyContext)
      (Display * dpy, GLXContext ctx);

      Bool(*glXMakeCurrent)
      (Display * dpy, GLXDrawable drawable, GLXContext ctx);

    void (*glXSwapBuffers)
      (Display * dpy, GLXDrawable drawable);

    int (*glXSwapIntervalSGI) (int interval);
      GLint(*glXSwapIntervalMESA) (unsigned interval);
      GLint(*glXGetSwapIntervalMESA) (void);
};

/* OpenGL functions */
extern int X11_GL_LoadLibrary(_THIS, const char *path);
extern void *X11_GL_GetProcAddress(_THIS, const char *proc);
extern void X11_GL_UnloadLibrary(_THIS);
extern XVisualInfo *X11_GL_GetVisual(_THIS, Display * display, int screen);
extern SDL_GLContext X11_GL_CreateContext(_THIS, SDL_Window * window);
extern int X11_GL_MakeCurrent(_THIS, SDL_Window * window,
                              SDL_GLContext context);
extern int X11_GL_SetSwapInterval(_THIS, int interval);
extern int X11_GL_GetSwapInterval(_THIS);
extern void X11_GL_SwapWindow(_THIS, SDL_Window * window);
extern void X11_GL_DeleteContext(_THIS, SDL_GLContext context);

#endif /* SDL_VIDEO_OPENGL_GLX */

#endif /* _SDL_x11opengl_h */

/* vi: set ts=4 sw=4 expandtab: */


text:
package org.apache.kafka.common.requests;

import org.apache.kafka.common.protocol.ApiKeys;
import org.apache.kafka.common.protocol.Errors;
import org.apache.kafka.common.protocol.types.Schema;
import org.apache.kafka.common.protocol.types.Struct;

import java.nio.ByteBuffer;
import java.util.Collections;

public class ListGroupsRequest extends AbstractRequest {

    /* List groups api */
    private static final Schema LIST_GROUPS_REQUEST_V0 = new Schema();

    /* v1 request is the same as v0. Throttle time has been added to response */
    private static final Schema LIST_GROUPS_REQUEST_V1 = LIST_GROUPS_REQUEST_V0;

    public static Schema[] schemaVersions() {
        return new Schema[] {LIST_GROUPS_REQUEST_V0, LIST_GROUPS_REQUEST_V1};
    }

    public static class Builder extends AbstractRequest.Builder<ListGroupsRequest> {
        public Builder() {
            super(ApiKeys.LIST_GROUPS);
        }

        @Override
        public ListGroupsRequest build(short version) {
            return new ListGroupsRequest(version);
        }

        @Override
        public String toString() {
            return "(type=ListGroupsRequest)";
        }
    }

    public ListGroupsRequest(short version) {
        super(version);
    }

    public ListGroupsRequest(Struct struct, short versionId) {
        super(versionId);
    }

    @Override
    public ListGroupsResponse getErrorResponse(int throttleTimeMs, Throwable e) {
        short versionId = version();
        switch (versionId) {
            case 0:
                return new ListGroupsResponse(Errors.forException(e), Collections.<ListGroupsResponse.Group>emptyList());
            case 1:
                return new ListGroupsResponse(throttleTimeMs, Errors.forException(e), Collections.<ListGroupsResponse.Group>emptyList());
            default:
                throw new IllegalArgumentException(String.format("Version %d is not valid. Valid versions for %s are 0 to %d",
                        versionId, this.getClass().getSimpleName(), ApiKeys.LIST_GROUPS.latestVersion()));
        }
    }

    public static ListGroupsRequest parse(ByteBuffer buffer, short version) {
        return new ListGroupsRequest(ApiKeys.LIST_GROUPS.parseRequest(version, buffer), version);
    }

    @Override
    protected Struct toStruct() {
        return new Struct(ApiKeys.LIST_GROUPS.requestSchema(version()));
    }
}