Serenity Operating System
at portability 216 lines 7.9 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * 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 are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (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 <LibCore/Gzip.h> 28#include <LibCore/HttpJob.h> 29#include <LibCore/HttpResponse.h> 30#include <LibCore/TCPSocket.h> 31#include <stdio.h> 32#include <unistd.h> 33 34//#define CHTTPJOB_DEBUG 35 36namespace Core { 37 38static ByteBuffer handle_content_encoding(const ByteBuffer& buf, const String& content_encoding) 39{ 40#ifdef CHTTPJOB_DEBUG 41 dbg() << "CHttpJob::handle_content_encoding: buf has content_encoding = " << content_encoding; 42#endif 43 44 if (content_encoding == "gzip") { 45 if (!CGzip::is_compressed(buf)) { 46 dbg() << "CHttpJob::handle_content_encoding: buf is not gzip compressed!"; 47 } 48 49#ifdef CHTTPJOB_DEBUG 50 dbg() << "CHttpJob::handle_content_encoding: buf is gzip compressed!"; 51#endif 52 53 auto uncompressed = CGzip::decompress(buf); 54 if (!uncompressed.has_value()) { 55 dbg() << "CHttpJob::handle_content_encoding: Gzip::decompress() failed. Returning original buffer."; 56 return buf; 57 } 58 59#ifdef CHTTPJOB_DEBUG 60 dbg() << "CHttpJob::handle_content_encoding: Gzip::decompress() successful.\n" 61 << " Input size = " << buf.size() << "\n" 62 << " Output size = " << uncompressed.value().size(); 63#endif 64 65 return uncompressed.value(); 66 } 67 68 return buf; 69} 70 71HttpJob::HttpJob(const HttpRequest& request) 72 : m_request(request) 73{ 74} 75 76HttpJob::~HttpJob() 77{ 78} 79 80void HttpJob::on_socket_connected() 81{ 82 auto raw_request = m_request.to_raw_request(); 83#if 0 84 dbg() << "CHttpJob: raw_request:"; 85 dbg() << String::copy(raw_request).characters(); 86#endif 87 88 bool success = m_socket->send(raw_request); 89 if (!success) 90 return deferred_invoke([this](auto&) { did_fail(NetworkJob::Error::TransmissionFailed); }); 91 92 m_socket->on_ready_to_read = [&] { 93 if (is_cancelled()) 94 return; 95 if (m_state == State::InStatus) { 96 if (!m_socket->can_read_line()) 97 return; 98 auto line = m_socket->read_line(PAGE_SIZE); 99 if (line.is_null()) { 100 fprintf(stderr, "CHttpJob: Expected HTTP status\n"); 101 return deferred_invoke([this](auto&) { did_fail(NetworkJob::Error::TransmissionFailed); }); 102 } 103 auto parts = String::copy(line, Chomp).split(' '); 104 if (parts.size() < 3) { 105 fprintf(stderr, "CHttpJob: Expected 3-part HTTP status, got '%s'\n", line.data()); 106 return deferred_invoke([this](auto&) { did_fail(NetworkJob::Error::ProtocolFailed); }); 107 } 108 bool ok; 109 m_code = parts[1].to_uint(ok); 110 if (!ok) { 111 fprintf(stderr, "CHttpJob: Expected numeric HTTP status\n"); 112 return deferred_invoke([this](auto&) { did_fail(NetworkJob::Error::ProtocolFailed); }); 113 } 114 m_state = State::InHeaders; 115 return; 116 } 117 if (m_state == State::InHeaders) { 118 if (!m_socket->can_read_line()) 119 return; 120 auto line = m_socket->read_line(PAGE_SIZE); 121 if (line.is_null()) { 122 fprintf(stderr, "CHttpJob: Expected HTTP header\n"); 123 return did_fail(NetworkJob::Error::ProtocolFailed); 124 } 125 auto chomped_line = String::copy(line, Chomp); 126 if (chomped_line.is_empty()) { 127 m_state = State::InBody; 128 return; 129 } 130 auto parts = chomped_line.split(':'); 131 if (parts.is_empty()) { 132 fprintf(stderr, "CHttpJob: Expected HTTP header with key/value\n"); 133 return deferred_invoke([this](auto&) { did_fail(NetworkJob::Error::ProtocolFailed); }); 134 } 135 auto name = parts[0]; 136 if (chomped_line.length() < name.length() + 2) { 137 fprintf(stderr, "CHttpJob: Malformed HTTP header: '%s' (%zu)\n", chomped_line.characters(), chomped_line.length()); 138 return deferred_invoke([this](auto&) { did_fail(NetworkJob::Error::ProtocolFailed); }); 139 } 140 auto value = chomped_line.substring(name.length() + 2, chomped_line.length() - name.length() - 2); 141 m_headers.set(name, value); 142#ifdef CHTTPJOB_DEBUG 143 dbg() << "CHttpJob: [" << name << "] = '" << value << "'"; 144#endif 145 return; 146 } 147 ASSERT(m_state == State::InBody); 148 ASSERT(m_socket->can_read()); 149 auto payload = m_socket->receive(64 * KB); 150 if (!payload) { 151 if (m_socket->eof()) 152 return finish_up(); 153 return deferred_invoke([this](auto&) { did_fail(NetworkJob::Error::ProtocolFailed); }); 154 } 155 m_received_buffers.append(payload); 156 m_received_size += payload.size(); 157 158 auto content_length_header = m_headers.get("Content-Length"); 159 if (content_length_header.has_value()) { 160 bool ok; 161 if (m_received_size >= content_length_header.value().to_uint(ok) && ok) 162 return finish_up(); 163 } 164 }; 165} 166 167void HttpJob::finish_up() 168{ 169 m_state = State::Finished; 170 auto flattened_buffer = ByteBuffer::create_uninitialized(m_received_size); 171 u8* flat_ptr = flattened_buffer.data(); 172 for (auto& received_buffer : m_received_buffers) { 173 memcpy(flat_ptr, received_buffer.data(), received_buffer.size()); 174 flat_ptr += received_buffer.size(); 175 } 176 m_received_buffers.clear(); 177 178 auto content_encoding = m_headers.get("Content-Encoding"); 179 if (content_encoding.has_value()) { 180 flattened_buffer = handle_content_encoding(flattened_buffer, content_encoding.value()); 181 } 182 183 auto response = HttpResponse::create(m_code, move(m_headers), move(flattened_buffer)); 184 deferred_invoke([this, response](auto&) { 185 did_finish(move(response)); 186 }); 187} 188 189void HttpJob::start() 190{ 191 ASSERT(!m_socket); 192 m_socket = TCPSocket::construct(this); 193 m_socket->on_connected = [this] { 194#ifdef CHTTPJOB_DEBUG 195 dbg() << "CHttpJob: on_connected callback"; 196#endif 197 on_socket_connected(); 198 }; 199 bool success = m_socket->connect(m_request.url().host(), m_request.url().port()); 200 if (!success) { 201 deferred_invoke([this](auto&) { 202 return did_fail(NetworkJob::Error::ConnectionFailed); 203 }); 204 } 205} 206 207void HttpJob::shutdown() 208{ 209 if (!m_socket) 210 return; 211 m_socket->on_ready_to_read = nullptr; 212 m_socket->on_connected = nullptr; 213 remove_child(*m_socket); 214 m_socket = nullptr; 215} 216}