2 * Copyright (C) 2011 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "HTTPRequest.h"
29 #include <wtf/text/CString.h>
31 using namespace WebCore;
35 PassRefPtr<HTTPRequest> HTTPRequest::parseHTTPRequestFromBuffer(const char* data, size_t length, String& failureReason)
38 failureReason = "No data to parse.";
42 // Request we will be building.
43 RefPtr<HTTPRequest> request = HTTPRequest::create();
45 // Advance a pointer through the data as needed.
46 const char* pos = data;
47 size_t remainingLength = length;
49 // 1. Parse Method + URL.
50 size_t requestLineLength = request->parseRequestLine(pos, remainingLength, failureReason);
51 if (!requestLineLength)
53 pos += requestLineLength;
54 remainingLength -= requestLineLength;
56 // 2. Parse HTTP Headers.
57 size_t headersLength = request->parseHeaders(pos, remainingLength, failureReason);
61 remainingLength -= headersLength;
63 // 3. Parse HTTP Data.
64 size_t dataLength = request->parseRequestBody(pos, remainingLength);
66 remainingLength -= dataLength;
68 // We should have processed the entire input.
69 ASSERT(!remainingLength);
70 return request.release();
73 size_t HTTPRequest::parseRequestLine(const char* data, size_t length, String& failureReason)
76 size_t result = parseHTTPRequestLine(data, length, failureReason, m_requestMethod, url, m_httpVersion);
77 m_url = KURL(KURL(), url);
81 size_t HTTPRequest::parseHeaders(const char* data, size_t length, String& failureReason)
84 const char* end = data + length;
87 for (; p < data + length; p++) {
88 size_t consumedLength = parseHTTPHeader(p, end - p, failureReason, name, value);
94 m_headerFields.add(name, value);
99 size_t HTTPRequest::parseRequestBody(const char* data, size_t length)
101 return parseHTTPRequestBody(data, length, m_body);
104 HTTPRequest::HTTPRequest()
105 : m_httpVersion(WebCore::Unknown)
109 HTTPRequest::HTTPRequest(const String& requestMethod, const KURL& url, HTTPVersion version)
111 , m_httpVersion(version)
112 , m_requestMethod(requestMethod)
116 HTTPRequest::~HTTPRequest()
120 } // namespace WebCore