void WebRequest::process (void) { int index; static int count = 0; char buffer[100]; WebPage *page; log (LOG_WEBSOCKET, "Process: %s, %s, %s", method, path, protocol); for (index = 0; index < number_of_arguments; index ++) { log (LOG_WEBSOCKET, " Arg : %-15s = %s", argument_key[index], argument_value[index]); } for (index = 0; index < number_of_headers; index ++) { log (LOG_WEBSOCKET, " Head: %-15s = %s", header_key[index], header_value[index]); } page = WebPage::find (path); if (page) { log (LOG_WEBSOCKET, " Page = %p", page); if (page->process (this)) { end_response (); } } else { page = WebPage::open (path); if ((page) && (page->process (this))) { end_response (); } else { add_response_part ("request_path", path); add_response_part ("page_left", ""); add_response_part ("page_right", "Page Not Found : ${request_path}"); add_template_response ("${page_layout}", strlen ("${page_layout}")); set_response_code (404); end_response (); } } socket->close_when_written (); }
static void write_connection(struct connection *cn) { struct pool *p; int m, n; p = &cn->output; do { n = p->end - p->start; if (n == 0) { #if defined LINUX_SENDFILE || defined FREEBSD_SENDFILE if (sendfile_connection(cn) == -1) { close_connection(cn); return; } if (cn->left == 0) end_response(cn); return; #else p->start = p->end = p->floor; n = fill_connection(cn); if (n == -1) { close_connection(cn); return; } if (n == 0) { end_response(cn); return; } #endif } m = write(cn->fd, p->start, n); if (debug) log_d("write_connection: %d %d %d %d", cn->fd, (int) (p->start - p->floor), n, m); if (m == -1) { if (errno == EAGAIN) return; if (debug) lerror("write"); close_connection(cn); return; } cn->t = current_time; cn->nwritten += m; p->start += m; } while (n == m && tuning.greedy); }
void send_response() { str host = parse_host(client.request,find_header(client.request->headers,_("Host"))); str method = parse_method(client.request); begin_response(); client.response->status = host && method ? dispatch_method(method) : error_handler(400); end_response(); disconnect(); }
void Client_connection::recv_response(buffer_t buf) { if (buf->empty()) { end_response({Error::NO_REPLY}); return; } const std::string data{(char*) buf->data(), buf->size()}; // restart timer since we got data if(timer_.is_running()) timer_.restart(timeout_dur_); // create response if not exist if(res_ == nullptr) { try { res_ = make_response(data); // this also parses } catch(...) { end_response({Error::INVALID}); return; } } // if there already is a response else { // this is the case when Status line is received, but not yet headers. if(not res_->headers_complete() && req_->method() != HEAD) { *res_ << data; res_->parse(); } // here we assume all headers has already been received (could not be true?) else { // add chunks of body data res_->add_chunk(data); } } const auto& header = res_->header(); // TODO: Temporary, not good enough // if(res_->is_complete()) // Assume we want some headers if(!header.is_empty()) { if(header.has_field(header::Content_Length)) { try { const unsigned conlen = std::stoul(std::string(header.value(header::Content_Length))); const unsigned body_size = res_->body().size(); //printf("<http::Connection> [%s] Data: %u ConLen: %u Body:%u\n", // req_->uri().to_string().to_string().c_str(), data.size(), conlen, body_size); // risk buffering forever if no timeout if(body_size == conlen) { end_response(); } else if(body_size > conlen) { end_response({Error::INVALID}); } } catch(...) { end_response({Error::INVALID}); } } else end_response(); } else if(req_->method() == HEAD) { end_response(); } }