|
|
| version 1.1, 2005/11/24 12:28:48 | version 1.39, 2026/10/16 13:55:16 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: commonly used functions. | Parser: commonly used functions. |
| Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru> |
| */ | */ |
| #ifndef PA_HTTP_H | #ifndef PA_HTTP_H |
| #define PA_HTTP_H | #define PA_HTTP_H |
| static const char * const IDENT_HTTP_H="$Date$"; | #define IDENT_PA_HTTP_H "$Id$" |
| #include "pa_vstring.h" | #include "pa_vstring.h" |
| #include "pa_vint.h" | #include "pa_vint.h" |
| #include "pa_vhash.h" | #include "pa_vhash.h" |
| #include "pa_vtable.h" | #include "pa_vtable.h" |
| #include "pa_socks.h" | #include "pa_socks.h" |
| #include "pa_charset.h" | |
| #include "pa_request.h" | |
| #ifdef CYGWIN | #define HTTP_COOKIES_NAME "cookies" |
| #define _GNU_H_WINDOWS32_SOCKETS | |
| // for PASCAL | #ifdef _MSC_VER |
| #include <windows.h> | #include <windows.h> |
| // SOCKET | |
| typedef u_int SOCKET; | |
| int PASCAL closesocket(SOCKET); | |
| #else | |
| # if defined(WIN32) | |
| # include <windows.h> | |
| # else | |
| # define closesocket close | |
| # endif | |
| #endif | #endif |
| const char* pa_form2string(HashStringValue& form); | |
| #ifndef DOXYGEN | #ifndef DOXYGEN |
| struct File_read_http_result { | struct File_read_http_result { |
| char *str; size_t length; | char *str; size_t length; |
| Line 39 struct File_read_http_result { | Line 31 struct File_read_http_result { |
| }; | }; |
| #endif | #endif |
| File_read_http_result pa_internal_file_read_http(Request_charsets& charsets, | class HTTP_Headers { |
| const String& file_spec, | public: |
| bool as_text, | class Header { |
| HashStringValue *options=0); | 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<Header> 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.clear(); | |
| 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); | |
| /*** 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<HTTP_Headers::Header> &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); | |
| /// bytes received after the request headers, not processed yet | |
| String::C unprocessed(); | |
| /// receives up to size bytes, returns 0 if the connection is closed | |
| size_t receive(char *, size_t); | |
| void set_nodelay(); | |
| void set_send_timeout(int timeout_secs); | |
| }; | |
| 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 | #endif |