Home | History | Annotate | Download | only in network
      1 /*
      2  * Copyright (C) 2003, 2006 Apple Computer, Inc.  All rights reserved.
      3  * Copyright (C) 2009, 2012 Google Inc. All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include "config.h"
     28 #include "core/platform/network/ResourceRequest.h"
     29 
     30 #include "core/platform/network/ResourceRequest.h"
     31 
     32 namespace WebCore {
     33 
     34 double ResourceRequest::s_defaultTimeoutInterval = INT_MAX;
     35 
     36 PassOwnPtr<ResourceRequest> ResourceRequest::adopt(PassOwnPtr<CrossThreadResourceRequestData> data)
     37 {
     38     OwnPtr<ResourceRequest> request = adoptPtr(new ResourceRequest());
     39     request->setURL(data->m_url);
     40     request->setCachePolicy(data->m_cachePolicy);
     41     request->setTimeoutInterval(data->m_timeoutInterval);
     42     request->setFirstPartyForCookies(data->m_firstPartyForCookies);
     43     request->setHTTPMethod(data->m_httpMethod);
     44     request->setPriority(data->m_priority);
     45 
     46     request->m_httpHeaderFields.adopt(data->m_httpHeaders.release());
     47 
     48     request->setHTTPBody(data->m_httpBody);
     49     request->setAllowCookies(data->m_allowCookies);
     50     request->setHasUserGesture(data->m_hasUserGesture);
     51     request->setDownloadToFile(data->m_downloadToFile);
     52     request->setRequestorID(data->m_requestorID);
     53     request->setRequestorProcessID(data->m_requestorProcessID);
     54     request->setAppCacheHostID(data->m_appCacheHostID);
     55     request->setTargetType(data->m_targetType);
     56     return request.release();
     57 }
     58 
     59 PassOwnPtr<CrossThreadResourceRequestData> ResourceRequest::copyData() const
     60 {
     61     OwnPtr<CrossThreadResourceRequestData> data = adoptPtr(new CrossThreadResourceRequestData());
     62     data->m_url = url().copy();
     63     data->m_cachePolicy = cachePolicy();
     64     data->m_timeoutInterval = timeoutInterval();
     65     data->m_firstPartyForCookies = firstPartyForCookies().copy();
     66     data->m_httpMethod = httpMethod().isolatedCopy();
     67     data->m_httpHeaders = httpHeaderFields().copyData();
     68     data->m_priority = priority();
     69 
     70     if (m_httpBody)
     71         data->m_httpBody = m_httpBody->deepCopy();
     72     data->m_allowCookies = m_allowCookies;
     73     data->m_hasUserGesture = m_hasUserGesture;
     74     data->m_downloadToFile = m_downloadToFile;
     75     data->m_requestorID = m_requestorID;
     76     data->m_requestorProcessID = m_requestorProcessID;
     77     data->m_appCacheHostID = m_appCacheHostID;
     78     data->m_targetType = m_targetType;
     79     return data.release();
     80 }
     81 
     82 bool ResourceRequest::isEmpty() const
     83 {
     84     return m_url.isEmpty();
     85 }
     86 
     87 bool ResourceRequest::isNull() const
     88 {
     89     return m_url.isNull();
     90 }
     91 
     92 const KURL& ResourceRequest::url() const
     93 {
     94     return m_url;
     95 }
     96 
     97 void ResourceRequest::setURL(const KURL& url)
     98 {
     99     m_url = url;
    100 }
    101 
    102 void ResourceRequest::removeCredentials()
    103 {
    104     if (m_url.user().isEmpty() && m_url.pass().isEmpty())
    105         return;
    106 
    107     m_url.setUser(String());
    108     m_url.setPass(String());
    109 }
    110 
    111 ResourceRequestCachePolicy ResourceRequest::cachePolicy() const
    112 {
    113     return m_cachePolicy;
    114 }
    115 
    116 void ResourceRequest::setCachePolicy(ResourceRequestCachePolicy cachePolicy)
    117 {
    118     m_cachePolicy = cachePolicy;
    119 }
    120 
    121 double ResourceRequest::timeoutInterval() const
    122 {
    123     return m_timeoutInterval;
    124 }
    125 
    126 void ResourceRequest::setTimeoutInterval(double timeoutInterval)
    127 {
    128     m_timeoutInterval = timeoutInterval;
    129 }
    130 
    131 const KURL& ResourceRequest::firstPartyForCookies() const
    132 {
    133     return m_firstPartyForCookies;
    134 }
    135 
    136 void ResourceRequest::setFirstPartyForCookies(const KURL& firstPartyForCookies)
    137 {
    138     m_firstPartyForCookies = firstPartyForCookies;
    139 }
    140 
    141 const String& ResourceRequest::httpMethod() const
    142 {
    143     return m_httpMethod;
    144 }
    145 
    146 void ResourceRequest::setHTTPMethod(const String& httpMethod)
    147 {
    148     m_httpMethod = httpMethod;
    149 }
    150 
    151 const HTTPHeaderMap& ResourceRequest::httpHeaderFields() const
    152 {
    153     return m_httpHeaderFields;
    154 }
    155 
    156 String ResourceRequest::httpHeaderField(const AtomicString& name) const
    157 {
    158     return m_httpHeaderFields.get(name);
    159 }
    160 
    161 String ResourceRequest::httpHeaderField(const char* name) const
    162 {
    163     return m_httpHeaderFields.get(name);
    164 }
    165 
    166 void ResourceRequest::setHTTPHeaderField(const AtomicString& name, const String& value)
    167 {
    168     m_httpHeaderFields.set(name, value);
    169 }
    170 
    171 void ResourceRequest::setHTTPHeaderField(const char* name, const String& value)
    172 {
    173     setHTTPHeaderField(AtomicString(name), value);
    174 }
    175 
    176 void ResourceRequest::clearHTTPAuthorization()
    177 {
    178     m_httpHeaderFields.remove("Authorization");
    179 }
    180 
    181 void ResourceRequest::clearHTTPContentType()
    182 {
    183     m_httpHeaderFields.remove("Content-Type");
    184 }
    185 
    186 void ResourceRequest::clearHTTPReferrer()
    187 {
    188     m_httpHeaderFields.remove("Referer");
    189 }
    190 
    191 void ResourceRequest::clearHTTPOrigin()
    192 {
    193     m_httpHeaderFields.remove("Origin");
    194 }
    195 
    196 void ResourceRequest::clearHTTPUserAgent()
    197 {
    198     m_httpHeaderFields.remove("User-Agent");
    199 }
    200 
    201 void ResourceRequest::clearHTTPAccept()
    202 {
    203     m_httpHeaderFields.remove("Accept");
    204 }
    205 
    206 FormData* ResourceRequest::httpBody() const
    207 {
    208     return m_httpBody.get();
    209 }
    210 
    211 void ResourceRequest::setHTTPBody(PassRefPtr<FormData> httpBody)
    212 {
    213     m_httpBody = httpBody;
    214 }
    215 
    216 bool ResourceRequest::allowCookies() const
    217 {
    218     return m_allowCookies;
    219 }
    220 
    221 void ResourceRequest::setAllowCookies(bool allowCookies)
    222 {
    223     m_allowCookies = allowCookies;
    224 }
    225 
    226 ResourceLoadPriority ResourceRequest::priority() const
    227 {
    228     return m_priority;
    229 }
    230 
    231 void ResourceRequest::setPriority(ResourceLoadPriority priority)
    232 {
    233     m_priority = priority;
    234 }
    235 
    236 void ResourceRequest::addHTTPHeaderField(const AtomicString& name, const String& value)
    237 {
    238     HTTPHeaderMap::AddResult result = m_httpHeaderFields.add(name, value);
    239     if (!result.isNewEntry)
    240         result.iterator->value = result.iterator->value + ',' + value;
    241 }
    242 
    243 void ResourceRequest::addHTTPHeaderFields(const HTTPHeaderMap& headerFields)
    244 {
    245     HTTPHeaderMap::const_iterator end = headerFields.end();
    246     for (HTTPHeaderMap::const_iterator it = headerFields.begin(); it != end; ++it)
    247         addHTTPHeaderField(it->key, it->value);
    248 }
    249 
    250 bool equalIgnoringHeaderFields(const ResourceRequest& a, const ResourceRequest& b)
    251 {
    252     if (a.url() != b.url())
    253         return false;
    254 
    255     if (a.cachePolicy() != b.cachePolicy())
    256         return false;
    257 
    258     if (a.timeoutInterval() != b.timeoutInterval())
    259         return false;
    260 
    261     if (a.firstPartyForCookies() != b.firstPartyForCookies())
    262         return false;
    263 
    264     if (a.httpMethod() != b.httpMethod())
    265         return false;
    266 
    267     if (a.allowCookies() != b.allowCookies())
    268         return false;
    269 
    270     if (a.priority() != b.priority())
    271         return false;
    272 
    273     FormData* formDataA = a.httpBody();
    274     FormData* formDataB = b.httpBody();
    275 
    276     if (!formDataA)
    277         return !formDataB;
    278     if (!formDataB)
    279         return !formDataA;
    280 
    281     if (*formDataA != *formDataB)
    282         return false;
    283 
    284     return true;
    285 }
    286 
    287 bool ResourceRequest::compare(const ResourceRequest& a, const ResourceRequest& b)
    288 {
    289     if (!equalIgnoringHeaderFields(a, b))
    290         return false;
    291 
    292     if (a.httpHeaderFields() != b.httpHeaderFields())
    293         return false;
    294 
    295     return true;
    296 }
    297 
    298 bool ResourceRequest::isConditional() const
    299 {
    300     return (m_httpHeaderFields.contains("If-Match") ||
    301             m_httpHeaderFields.contains("If-Modified-Since") ||
    302             m_httpHeaderFields.contains("If-None-Match") ||
    303             m_httpHeaderFields.contains("If-Range") ||
    304             m_httpHeaderFields.contains("If-Unmodified-Since"));
    305 }
    306 
    307 double ResourceRequest::defaultTimeoutInterval()
    308 {
    309     return s_defaultTimeoutInterval;
    310 }
    311 
    312 void ResourceRequest::setDefaultTimeoutInterval(double timeoutInterval)
    313 {
    314     s_defaultTimeoutInterval = timeoutInterval;
    315 }
    316 
    317 void ResourceRequest::initialize(const KURL& url, ResourceRequestCachePolicy cachePolicy)
    318 {
    319     m_url = url;
    320     m_cachePolicy = cachePolicy;
    321     m_timeoutInterval = s_defaultTimeoutInterval;
    322     m_httpMethod = "GET";
    323     m_allowCookies = true;
    324     m_reportUploadProgress = false;
    325     m_reportLoadTiming = false;
    326     m_reportRawHeaders = false;
    327     m_hasUserGesture = false;
    328     m_downloadToFile = false;
    329     m_priority = ResourceLoadPriorityLow;
    330     m_requestorID = 0;
    331     m_requestorProcessID = 0;
    332     m_appCacheHostID = 0;
    333     m_targetType = TargetIsUnspecified;
    334 }
    335 
    336 // This is used by the loader to control the number of issued parallel load requests.
    337 unsigned initializeMaximumHTTPConnectionCountPerHost()
    338 {
    339     // The chromium network stack already handles limiting the number of
    340     // parallel requests per host, so there's no need to do it here.  Therefore,
    341     // this is set to a high value that should never be hit in practice.
    342     return 10000;
    343 }
    344 
    345 }
    346