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 "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 nullptr; 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 nullptr; 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 nullptr; 58 pos += headersLength; 59 remainingLength -= headersLength; 60 61 // 3. Parse HTTP Data. 62 size_t dataLength = request->parseRequestBody(pos, remainingLength); 63 remainingLength -= dataLength; 64 65 // We should have processed the entire input. 66 ASSERT(!remainingLength); 67 return request.release(); 68 } 69 70 size_t HTTPRequest::parseRequestLine(const char* data, size_t length, String& failureReason) 71 { 72 String url; 73 size_t result = parseHTTPRequestLine(data, length, failureReason, m_requestMethod, url, m_httpVersion); 74 m_url = KURL(KURL(), url); 75 return result; 76 } 77 78 size_t HTTPRequest::parseHeaders(const char* data, size_t length, String& failureReason) 79 { 80 const char* p = data; 81 const char* end = data + length; 82 AtomicString name; 83 AtomicString value; 84 while (p < data + length) { 85 size_t consumedLength = parseHTTPHeader(p, end - p, failureReason, name, value); 86 if (!consumedLength) 87 return 0; 88 p += consumedLength; 89 if (name.isEmpty()) 90 break; 91 m_headerFields.add(name, value); 92 } 93 return p - data; 94 } 95 96 size_t HTTPRequest::parseRequestBody(const char* data, size_t length) 97 { 98 return parseHTTPRequestBody(data, length, m_body); 99 } 100 101 HTTPRequest::HTTPRequest() 102 : m_httpVersion(Unknown) 103 { 104 } 105 106 HTTPRequest::HTTPRequest(const String& requestMethod, const KURL& url, HTTPVersion version) 107 : m_url(url) 108 , m_httpVersion(version) 109 , m_requestMethod(requestMethod) 110 { 111 } 112 113 HTTPRequest::~HTTPRequest() 114 { 115 } 116 117 } // namespace WebCore 118