--- parser3/src/include/pa_http.h 2016/11/24 19:24:46 1.17 +++ parser3/src/include/pa_http.h 2024/11/04 03:53:25 1.35 @@ -1,14 +1,14 @@ /** @file Parser: commonly used functions. - Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) - Author: Alexandr Petrosian (http://paf.design.ru) + Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com) + Authors: Konstantin Morshnev , Alexandr Petrosian */ #ifndef PA_HTTP_H #define PA_HTTP_H -#define IDENT_PA_HTTP_H "$Id: pa_http.h,v 1.17 2016/11/24 19:24:46 moko Exp $" +#define IDENT_PA_HTTP_H "$Id: pa_http.h,v 1.35 2024/11/04 03:53:25 moko Exp $" #include "pa_vstring.h" #include "pa_vint.h" @@ -20,6 +20,15 @@ #define HTTP_COOKIES_NAME "cookies" +#ifdef _MSC_VER +#include +#define socklen_t int +#else +#define closesocket close +#define INVALID_SOCKET -1 +typedef int SOCKET; +#endif + #ifndef DOXYGEN struct File_read_http_result { char *str; size_t length; @@ -27,7 +36,7 @@ struct File_read_http_result { }; #endif -class ResponseHeaders { +class HTTP_Headers { public: class Header { public: @@ -46,30 +55,22 @@ public: Array
headers; String::Body content_type; + uint64_t content_length; - bool add_header(const char *line){ - const char *value=strchr(line, ':'); - - if(value && value != line){ // we need only headers, not the response code - Header header(str_upper(line, value-line), String::Body(value+1).trim(String::TRIM_BOTH, " \t\n\r")); + HTTP_Headers() : content_type(""), content_length(0){} - if(header.name == String::Body(HTTP_CONTENT_TYPE_UPPER) && content_type.is_empty()) - content_type=header.value; - - headers+=header; - - return true; - } - return false; - } + bool add_header(const char *line); void clear(){ headers.clear(); content_type=""; + content_length=0; } }; +/*** http part ***/ + Table* parse_cookies(Request& r, Table *cookies); void tables_update(HashStringValue& tables, const String::Body name, const String& value); @@ -77,4 +78,54 @@ char *pa_http_safe_header_name(const cha File_read_http_result pa_internal_file_read_http(Request& r, const String& file_spec, bool as_text, HashStringValue *options=0, bool transcode_text_result=true); +/*** httpd part ***/ + +//#define HTTPD_DEBUG + +class HTTPD_request; + +class HTTPD_Connection : public PA_Allocated { +public: + SOCKET sock; + const char *remote_addr; + HTTPD_request *request; + + HTTPD_Connection() : sock(INVALID_SOCKET), remote_addr(NULL), request(NULL){} + ~HTTPD_Connection(); + + Array &headers(); + + const char *method(); + const char *uri(); + const char *content_type(); + uint64_t content_length(); + + const char *query(){ + if(uri()){ + const char *result=strchr(uri(), '?'); + if(result++ && *result) + return result; + } + return NULL; + } + + bool accept(SOCKET, int); + bool read_header(); + size_t read_post(char *, size_t); + size_t send_body(const void *, size_t); +}; + +class HTTPD_Server : public PA_Allocated { +public: + enum HTTPD_MODE { + SEQUENTIAL, + PARALLEL, + MULTITHREADED + } static mode; + static const char *port; + + static void set_mode(const String&); + static SOCKET bind(const char *); +}; + #endif