std::unique_ptr<http::request> http::request_reader::accept_request() {
  std::string method;
  std::string request_uri;
  std::tie(method, request_uri) = read_request_line();
  auto headers = read_request_headers();
  return std::unique_ptr<http::request>(new http::request(method, request_uri, headers));
}
Beispiel #2
0
/** @brief Handles HTTP GET requests.
 *  Reads and parses the request, the uri, and dispatches the appropriate
 *  functions to serve content.
 *  @param conn_fd The connection file descriptor.
 *  @return none.
 */
void handle_request(int conn_fd)
{
	rio_t rio;
	char request[MAXLINE], uri[MAXLINE];
	char file_name[MAXLINE], cgi_args[MAXLINE];
	int is_static;
	struct stat st_buf;

	Rio_readinitb(&rio, conn_fd);

	/* Read first header line of incoming request. */
	if (rio_readlineb(&rio, request, MAXLINE) < 0) {
		dbg_printf("rio_readlineb error\n");
		return;
	}
	dbg_printf("Request: %s", request);

	if (parse_request_header(conn_fd, request, uri) != SUCCESS) {
		return;
	}

	if (read_request_headers(&rio) != SUCCESS) {
		return;
	}

	find_file(uri, file_name, cgi_args, &is_static);

	if (stat(file_name, &st_buf) < 0) {
		dbg_printf("404: File not found: %s\n", file_name);
		send_error_msg(conn_fd, "404", "File not found");
		return;
	}

	/* Handle static content */
	if (is_static) {
		if (!(S_ISREG(st_buf.st_mode)) || !(S_IRUSR & st_buf.st_mode)) {
			dbg_printf("403: Can't read the file: %s\n", file_name);
			send_error_msg(conn_fd, "403", "Can't read the file.");
			return;
		}
		serve_static(conn_fd, file_name, st_buf.st_size);
	}
	/* Handle dynamic content */
	else {
		if (!(S_ISREG(st_buf.st_mode)) || !(S_IXUSR & st_buf.st_mode)) {
			dbg_printf("403: Can't run the CGI program: %s\n", file_name);
			send_error_msg(conn_fd, "403", "Can't run the CGI program.");
	 		return;
		}
		serve_dynamic(conn_fd, file_name, cgi_args);
	}
}