Home | History | Annotate | Download | only in network
      1 /*
      2  * Copyright (C) 2011 Apple Inc. All Rights Reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "core/platform/network/HTTPRequest.h"
     28 
     29 #include "wtf/text/CString.h"
     30 
     31 namespace WebCore {
     32 
     33 PassRefPtr<HTTPRequest> HTTPRequest::parseHTTPRequestFromBuffer(const char* data, size_t length, String& failureReason)
     34 {
     35     if (!length) {
     36         failureReason = "No data to parse.";
     37         return 0;
     38     }
     39 
     40     // Request we will be building.
     41     RefPtr<HTTPRequest> request = HTTPRequest::create();
     42 
     43     // Advance a pointer through the data as needed.
     44     const char* pos = data;
     45     size_t remainingLength = length;
     46 
     47     // 1. Parse Method + URL.
     48     size_t requestLineLength = request->parseRequestLine(pos, remainingLength, failureReason);
     49     if (!requestLineLength)
     50         return 0;
     51     pos += requestLineLength;
     52     remainingLength -= requestLineLength;
     53 
     54     // 2. Parse HTTP Headers.
     55     size_t headersLength = request->parseHeaders(pos, remainingLength, failureReason);
     56     if (!headersLength)
     57         return 0;
     58     pos += headersLength;
     59     remainingLength -= headersLength;
     60 
     61     // 3. Parse HTTP Data.
     62     size_t dataLength = request->parseRequestBody(pos, remainingLength);
     63     pos += dataLength;
     64     remainingLength -= dataLength;
     65 
     66     // We should have processed the entire input.
     67     ASSERT(!remainingLength);
     68     return request.release();
     69 }
     70 
     71 size_t HTTPRequest::parseRequestLine(const char* data, size_t length, String& failureReason)
     72 {
     73     String url;
     74     size_t result = parseHTTPRequestLine(data, length, failureReason, m_requestMethod, url, m_httpVersion);
     75     m_url = KURL(KURL(), url);
     76     return result;
     77 }
     78 
     79 size_t HTTPRequest::parseHeaders(const char* data, size_t length, String& failureReason)
     80 {
     81     const char* p = data;
     82     const char* end = data + length;
     83     AtomicString name;
     84     String value;
     85     for (; p < data + length; p++) {
     86         size_t consumedLength = parseHTTPHeader(p, end - p, failureReason, name, value);
     87         if (!consumedLength)
     88             return 0;
     89         p += consumedLength;
     90         if (name.isEmpty())
     91             break;
     92         m_headerFields.add(name, value);
     93     }
     94     return p - data;
     95 }
     96 
     97 size_t HTTPRequest::parseRequestBody(const char* data, size_t length)
     98 {
     99     return parseHTTPRequestBody(data, length, m_body);
    100 }
    101 
    102 HTTPRequest::HTTPRequest()
    103     : m_httpVersion(Unknown)
    104 {
    105 }
    106 
    107 HTTPRequest::HTTPRequest(const String& requestMethod, const KURL& url, HTTPVersion version)
    108     : m_url(url)
    109     , m_httpVersion(version)
    110     , m_requestMethod(requestMethod)
    111 {
    112 }
    113 
    114 HTTPRequest::~HTTPRequest()
    115 {
    116 }
    117 
    118 } // namespace WebCore
    119