--- parser3/src/include/pa_http.h 2012/03/16 09:24:09 1.7 +++ parser3/src/include/pa_http.h 2020/12/19 22:34:21 1.32 @@ -1,35 +1,24 @@ /** @file Parser: commonly used functions. - Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2020 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ #ifndef PA_HTTP_H #define PA_HTTP_H -#define IDENT_PA_HTTP_H "$Id: pa_http.h,v 1.7 2012/03/16 09:24:09 moko Exp $" +#define IDENT_PA_HTTP_H "$Id: pa_http.h,v 1.32 2020/12/19 22:34:21 moko Exp $" #include "pa_vstring.h" #include "pa_vint.h" #include "pa_vhash.h" #include "pa_vtable.h" #include "pa_socks.h" +#include "pa_charset.h" +#include "pa_request.h" -#ifdef CYGWIN -#define _GNU_H_WINDOWS32_SOCKETS -// for PASCAL -#include -// SOCKET -typedef u_int SOCKET; -int PASCAL closesocket(SOCKET); -#else -# if defined(WIN32) -# include -# else -# define closesocket close -# endif -#endif +#define HTTP_COOKIES_NAME "cookies" #ifndef DOXYGEN struct File_read_http_result { @@ -38,12 +27,96 @@ struct File_read_http_result { }; #endif +class HTTP_Headers { +public: + class Header { + public: + String::Body name; + String::Body value; + + Header(String::Body aname, String::Body avalue) : name(aname), value(avalue) {} + + void transcode(Charset &charset, Charset &source){ + name=Charset::transcode(name, charset, source); + value=Charset::transcode(value, charset, source); + } + + }; + + Array
headers; + + String::Body content_type; + uint64_t content_length; + + HTTP_Headers() : content_type(""), content_length(0){} + + 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); + char *pa_http_safe_header_name(const char *name); -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); +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: + int sock; + const char *remote_addr; + HTTPD_request *request; + + HTTPD_Connection() : sock(-1), 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(int, 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 int bind(const char *); +}; #endif