41 #include "NptBufferedStreams.h" 42 #include "NptSockets.h" 44 #include "NptDynamicCast.h" 45 #include "NptVersion.h" 47 #include "NptThreads.h" 48 #include "NptAutomaticCleaner.h" 53 const unsigned int NPT_HTTP_DEFAULT_PORT = 80;
54 const unsigned int NPT_HTTPS_DEFAULT_PORT = 443;
55 const unsigned int NPT_HTTP_INVALID_PORT = 0;
57 const NPT_Timeout NPT_HTTP_CLIENT_DEFAULT_CONNECTION_TIMEOUT = 30000;
58 const NPT_Timeout NPT_HTTP_CLIENT_DEFAULT_IO_TIMEOUT = 30000;
59 const NPT_Timeout NPT_HTTP_CLIENT_DEFAULT_NAME_RESOLVER_TIMEOUT = 60000;
60 const unsigned int NPT_HTTP_CLIENT_DEFAULT_MAX_REDIRECTS = 20;
62 const NPT_Timeout NPT_HTTP_SERVER_DEFAULT_CONNECTION_TIMEOUT = NPT_TIMEOUT_INFINITE;
63 const NPT_Timeout NPT_HTTP_SERVER_DEFAULT_IO_TIMEOUT = 60000;
65 const unsigned int NPT_HTTP_CONNECTION_MANAGER_MAX_CONNECTION_POOL_SIZE = 5;
66 const unsigned int NPT_HTTP_CONNECTION_MANAGER_MAX_CONNECTION_AGE = 30;
67 const unsigned int NPT_HTTP_MAX_RECONNECTS = 10;
68 const unsigned int NPT_HTTP_MAX_100_RESPONSES = 10;
70 const int NPT_HTTP_PROTOCOL_MAX_LINE_LENGTH = 8192;
71 const int NPT_HTTP_PROTOCOL_MAX_HEADER_COUNT = 100;
73 #define NPT_HTTP_PROTOCOL_1_0 "HTTP/1.0" 74 #define NPT_HTTP_PROTOCOL_1_1 "HTTP/1.1" 75 #define NPT_HTTP_METHOD_GET "GET" 76 #define NPT_HTTP_METHOD_HEAD "HEAD" 77 #define NPT_HTTP_METHOD_POST "POST" 78 #define NPT_HTTP_METHOD_PUT "PUT" 79 #define NPT_HTTP_METHOD_OPTIONS "OPTIONS" 80 #define NPT_HTTP_METHOD_DELETE "DELETE" 81 #define NPT_HTTP_METHOD_TRACE "TRACE" 83 #define NPT_HTTP_HEADER_HOST "Host" 84 #define NPT_HTTP_HEADER_CONNECTION "Connection" 85 #define NPT_HTTP_HEADER_USER_AGENT "User-Agent" 86 #define NPT_HTTP_HEADER_SERVER "Server" 87 #define NPT_HTTP_HEADER_CONTENT_LENGTH "Content-Length" 88 #define NPT_HTTP_HEADER_CONTENT_TYPE "Content-Type" 89 #define NPT_HTTP_HEADER_CONTENT_ENCODING "Content-Encoding" 90 #define NPT_HTTP_HEADER_TRANSFER_ENCODING "Transfer-Encoding" 91 #define NPT_HTTP_HEADER_LOCATION "Location" 92 #define NPT_HTTP_HEADER_RANGE "Range" 93 #define NPT_HTTP_HEADER_CONTENT_RANGE "Content-Range" 94 #define NPT_HTTP_HEADER_COOKIE "Cookie" 95 #define NPT_HTTP_HEADER_ACCEPT_RANGES "Accept-Ranges" 96 #define NPT_HTTP_HEADER_CONTENT_RANGE "Content-Range" 97 #define NPT_HTTP_HEADER_AUTHORIZATION "Authorization" 99 #define NPT_HTTP_TRANSFER_ENCODING_CHUNKED "chunked" 102 const int NPT_ERROR_HTTP_INVALID_RESPONSE_LINE = NPT_ERROR_BASE_HTTP - 0;
103 const int NPT_ERROR_HTTP_INVALID_REQUEST_LINE = NPT_ERROR_BASE_HTTP - 1;
104 const int NPT_ERROR_HTTP_NO_PROXY = NPT_ERROR_BASE_HTTP - 2;
105 const int NPT_ERROR_HTTP_INVALID_REQUEST = NPT_ERROR_BASE_HTTP - 3;
106 const int NPT_ERROR_HTTP_METHOD_NOT_SUPPORTED = NPT_ERROR_BASE_HTTP - 4;
107 const int NPT_ERROR_HTTP_TOO_MANY_REDIRECTS = NPT_ERROR_BASE_HTTP - 5;
108 const int NPT_ERROR_HTTP_TOO_MANY_RECONNECTS = NPT_ERROR_BASE_HTTP - 6;
109 const int NPT_ERROR_HTTP_CANNOT_RESEND_BODY = NPT_ERROR_BASE_HTTP - 7;
111 #define NPT_HTTP_LINE_TERMINATOR "\r\n" 113 #if !defined(NPT_CONFIG_HTTP_DEFAULT_USER_AGENT) 114 #define NPT_CONFIG_HTTP_DEFAULT_USER_AGENT "Neptune/" NPT_NEPTUNE_VERSION_STRING 120 typedef unsigned int NPT_HttpStatusCode;
133 const char* query = NULL,
134 const char* fragment = NULL);
135 NPT_HttpUrl(
const char* url,
bool ignore_scheme =
false);
148 const char* GetStatusCodeString(NPT_HttpStatusCode status_code);
162 const NPT_String& GetName()
const {
return m_Name; }
163 const NPT_String& GetValue()
const {
return m_Value; }
164 NPT_Result SetName(
const char* name);
165 NPT_Result SetValue(
const char* value);
187 const NPT_String* GetHeaderValue(
const char* name)
const;
188 NPT_Result SetHeader(
const char* name,
const char* value,
bool replace=
true);
189 NPT_Result AddHeader(
const char* name,
const char* value);
190 NPT_Result RemoveHeader(
const char* name);
209 bool update_content_length =
false);
210 NPT_Result SetInputStream(
const void* data, NPT_Size size);
211 NPT_Result SetInputStream(
const NPT_String&
string);
212 NPT_Result SetInputStream(
const char*
string);
218 NPT_Result SetContentLength(NPT_LargeSize length);
219 NPT_Result SetContentType(
const char* type);
220 NPT_Result SetContentEncoding(
const char* encoding);
221 NPT_Result SetTransferEncoding(
const char* encoding);
222 NPT_LargeSize GetContentLength() {
return m_ContentLength; }
223 const NPT_String& GetContentType() {
return m_ContentType; }
224 const NPT_String& GetContentEncoding() {
return m_ContentEncoding; }
225 const NPT_String& GetTransferEncoding() {
return m_TransferEncoding;}
226 bool ContentLengthIsKnown() {
return m_ContentLengthIsKnown; }
231 NPT_LargeSize m_ContentLength;
235 bool m_ContentLengthIsKnown;
250 NPT_Result SetProtocol(
const char* protocol) {
251 m_Protocol = protocol;
292 const char* protocol = NPT_HTTP_PROTOCOL_1_0);
295 const char* protocol = NPT_HTTP_PROTOCOL_1_0);
299 const NPT_HttpUrl& GetUrl()
const {
return m_Url; }
301 NPT_Result SetUrl(
const char* url);
303 const NPT_String& GetMethod()
const {
return m_Method; }
304 virtual NPT_Result Emit(
NPT_OutputStream& stream,
bool use_proxy=
false)
const;
323 const char* reason_phrase,
324 const char* protocol = NPT_HTTP_PROTOCOL_1_0);
328 NPT_Result SetStatus(NPT_HttpStatusCode status_code,
329 const char* reason_phrase,
330 const char* protocol = NULL);
331 NPT_Result SetProtocol(
const char* protocol);
332 NPT_HttpStatusCode GetStatusCode()
const {
return m_StatusCode; }
333 const NPT_String& GetReasonPhrase()
const {
return m_ReasonPhrase; }
338 NPT_HttpStatusCode m_StatusCode;
350 m_HostName(hostname), m_Port(port) {}
352 const NPT_String& GetHostName()
const {
return m_HostName; }
353 void SetHostName(
const char* hostname) { m_HostName = hostname; }
354 NPT_UInt16
GetPort()
const {
return m_Port; }
355 void SetPort(NPT_UInt16 port) { m_Port = port; }
390 Config() : m_ConnectionTimeout( NPT_HTTP_CLIENT_DEFAULT_CONNECTION_TIMEOUT),
391 m_IoTimeout( NPT_HTTP_CLIENT_DEFAULT_CONNECTION_TIMEOUT),
392 m_NameResolverTimeout(NPT_HTTP_CLIENT_DEFAULT_NAME_RESOLVER_TIMEOUT),
393 m_MaxRedirects( NPT_HTTP_CLIENT_DEFAULT_MAX_REDIRECTS),
394 m_UserAgent( NPT_CONFIG_HTTP_DEFAULT_USER_AGENT) {}
395 NPT_Timeout m_ConnectionTimeout;
396 NPT_Timeout m_IoTimeout;
397 NPT_Timeout m_NameResolverTimeout;
398 NPT_Cardinal m_MaxRedirects;
408 virtual bool SupportsPersistence() {
return false; }
409 virtual bool IsRecycled() {
return false; }
410 virtual NPT_Result Recycle() {
delete this;
return NPT_SUCCESS; }
411 virtual NPT_Result Abort() {
return NPT_ERROR_NOT_IMPLEMENTED; }
426 Connection* connection) {
return client.TrackConnection(connection); }
434 bool use_proxy =
false);
458 const Config& GetConfig()
const {
return m_Config; }
460 NPT_Result SetProxy(
const char* http_proxy_hostname,
461 NPT_UInt16 http_proxy_port,
462 const char* https_proxy_hostname = NULL,
463 NPT_UInt16 https_proxy_port = 0);
465 NPT_Result SetConnector(
Connector* connector);
466 NPT_Result SetTimeouts(NPT_Timeout connection_timeout,
467 NPT_Timeout io_timeout,
468 NPT_Timeout name_resolver_timeout);
469 NPT_Result SetUserAgent(
const char* user_agent);
470 NPT_Result SetOptions(NPT_Flags options,
bool on);
474 NPT_Result TrackConnection(
Connection* connection);
482 bool m_ProxySelectorIsOwned;
484 bool m_ConnectorIsOwned;
511 NPT_Result GetInfo(
NPT_SocketInfo& info)
override {
return m_Socket->GetInfo(info); }
512 bool SupportsPersistence()
override {
return true; }
513 bool IsRecycled()
override {
return m_IsRecycled; }
514 NPT_Result Recycle()
override;
515 NPT_Result Abort()
override {
return m_Socket->Cancel(); }
552 NPT_Result Cleanup();
556 NPT_Cardinal m_MaxConnections;
557 NPT_Cardinal m_MaxConnectionAge;
578 m_LocalAddress = address;
581 m_RemoteAddress = address;
630 const char* mime_type =
"text/html",
634 const char* mime_type =
"text/html",
651 const char* extension;
652 const char* mime_type;
663 const char* file_root,
664 bool auto_dir =
false,
665 const char* auto_index = NULL);
673 static const char* GetDefaultContentType(
const char* extension);
677 void SetDefaultMimeType(
const char* mime_type) {
678 m_DefaultMimeType = mime_type;
680 void SetUseDefaultFileTypeMap(
bool use_default) {
681 m_UseDefaultFileTypeMap = use_default;
690 const char* GetContentType(
const NPT_String& filename);
697 bool m_UseDefaultFileTypeMap;
709 NPT_Timeout m_ConnectionTimeout;
710 NPT_Timeout m_IoTimeout;
712 NPT_UInt16 m_ListenPort;
717 NPT_HttpServer(NPT_UInt16 listen_port = NPT_HTTP_DEFAULT_PORT,
bool cancellable =
false);
719 NPT_UInt16 listen_port = NPT_HTTP_DEFAULT_PORT,
720 bool cancellable =
false);
725 const Config& GetConfig()
const {
return m_Config; }
726 NPT_Result SetListenPort(NPT_UInt16 port,
bool reuse_address =
true);
727 NPT_Result SetTimeouts(NPT_Timeout connection_timeout, NPT_Timeout io_timeout);
728 NPT_Result SetServerHeader(
const char* server_header);
733 NPT_Flags socket_flags = 0);
734 NPT_Result Loop(
bool cancellable_sockets=
true);
735 NPT_UInt16
GetPort() {
return m_BoundPort; }
744 bool include_children =
false,
745 bool transfer_ownership =
false);
761 bool include_children,
762 bool transfer_ownership =
false);
771 bool m_IncludeChildren;
772 bool m_HandlerIsOwned;
780 NPT_UInt16 m_BoundPort;
794 NPT_Timeout m_IoTimeout;
804 NPT_Result SetTimeout(NPT_Timeout io_timeout);
827 NPT_Result Read(
void* buffer,
828 NPT_Size bytes_to_read,
829 NPT_Size* bytes_read = NULL)
override;
830 NPT_Result Seek(NPT_Position offset)
override;
831 NPT_Result Tell(NPT_Position& offset)
override;
832 NPT_Result GetSize(NPT_LargeSize& size)
override;
833 NPT_Result GetAvailable(NPT_LargeSize& available)
override;
838 NPT_UInt32 m_CurrentChunkSize;
853 NPT_Result Write(
const void* buffer,
854 NPT_Size bytes_to_write,
855 NPT_Size* bytes_written = NULL)
override;
856 NPT_Result Seek(NPT_Position )
override {
return NPT_ERROR_NOT_SUPPORTED;}
857 NPT_Result Tell(NPT_Position& offset)
override {
return m_Stream.Tell(offset); }
858 NPT_Result Flush()
override {
return m_Stream.Flush(); }
865 #endif // _NPT_HTTP_H_ Definition: NptThreads.h:234
Definition: NptAutomaticCleaner.h:46
Definition: deflate.c:123
Definition: NptHttp.h:566
NPT_Result SetPort(NPT_UInt16 port)
Sets the port number of the URL.
Definition: NptUri.cpp:709
Definition: NptHttp.h:658
Definition: NptHttp.h:650
Definition: NptHttp.h:200
Definition: NptSockets.h:317
Definition: NptHttp.h:315
Definition: NptHttp.h:126
Definition: NptHttp.h:282
Definition: NptHttp.h:241
Definition: NptHttp.h:593
NPT_UInt16 GetPort() const
Returns the port number of the URL.
Definition: NptUri.h:189
Definition: NptThreads.h:76
Definition: NptNetwork.h:74
Definition: NptThreads.h:149
virtual void Completed(NPT_Result)
A notification method called by the server upon completing the processing of a request.
Definition: NptHttp.h:619
Definition: NptSockets.h:77
Definition: NptStreams.h:92
Definition: NptHttp.h:345
Definition: NptHttp.h:793
Definition: NptHttp.h:499
Definition: NptHttp.h:625
Definition: NptSockets.h:115
Definition: NptHttp.h:758
Definition: NptHttp.h:790
Definition: NptDataBuffer.h:44
Definition: NptHttp.h:389
NPT_String ToString(bool with_fragment=true) const override
Return the string representation of the URL.
Definition: NptHttp.cpp:89
Definition: NptHttp.h:144
Definition: NptHttp.h:708
NPT_Result Parse(const char *url, NPT_UInt16 default_port=0)
Parse a URL from its fully encoded form.
Definition: NptUri.cpp:489
Definition: NptHttp.h:845
Definition: NptHttp.h:492
Definition: NptHttp.h:386
Definition: NptHttp.h:365
Definition: NptHttp.h:402
Definition: NptHttp.h:414
Definition: NptStrings.h:57
Definition: NptHttp.h:705