fixed typo
[anytun.git] / Sockets / HTTPSocket.h
blob00bb3053f840c5bd420b6a38f3b037bc432f696e
1 /** \file HTTPSocket.h Class HTTPSocket definition.
2 ** \date 2004-04-06
3 ** \author grymse@alhem.net
4 **/
5 /*
6 Copyright (C) 2004-2007 Anders Hedstrom
8 This library is made available under the terms of the GNU GPL.
10 If you would like to use this library in a closed-source application,
11 a separate license agreement is available. For information about
12 the closed-source license agreement for the C++ sockets library,
13 please visit http://www.alhem.net/Sockets/license.html and/or
14 email license@alhem.net.
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public License
18 as published by the Free Software Foundation; either version 2
19 of the License, or (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #ifndef _SOCKETS_HTTPSocket_H
31 #define _SOCKETS_HTTPSocket_H
33 #include "sockets-config.h"
34 #include <map>
35 #include "TcpSocket.h"
37 #ifdef SOCKETS_NAMESPACE
38 namespace SOCKETS_NAMESPACE {
39 #endif
41 /** \defgroup http HTTP Sockets */
42 /** HTTP request/response base class.
43 \ingroup http */
44 class HTTPSocket : public TcpSocket
46 /** map to hold http header values. */
47 typedef std::map<std::string,std::string> string_m;
48 public:
49 HTTPSocket(ISocketHandler& );
50 ~HTTPSocket();
52 void OnRawData(const char *buf,size_t len);
53 void OnLine(const std::string& line);
55 /** Callback executes when first line has been received.
56 GetMethod, GetUrl/GetUri, and GetHttpVersion are valid when this callback is executed. */
57 virtual void OnFirst() = 0;
58 /** For each header line this callback is executed.
59 \param key Http header name
60 \param value Http header value */
61 virtual void OnHeader(const std::string& key,const std::string& value) = 0;
62 /** Callback fires when all http headers have been received. */
63 virtual void OnHeaderComplete() = 0;
64 /** Chunk of http body data recevied. */
65 virtual void OnData(const char *,size_t) = 0;
67 /** Get http method from incoming request, ie GET/POST/PUT etc */
68 const std::string& GetMethod();
69 /** Set http method to be used in request. */
70 void SetMethod(const std::string& x);
71 /** Get url from request. */
72 const std::string& GetUrl();
73 /** Set url to be used in outgoing request. */
74 void SetUrl(const std::string& x);
75 /** Get part of url before '?' character. */
76 const std::string& GetUri();
77 /** Now why would I need this when there is a SetUrl method? */
78 void SetUri(const std::string& x);
79 /** Get part of url after '?' character. */
80 const std::string& GetQueryString();
81 /** Get http version from incoming request/response. */
82 const std::string& GetHttpVersion();
83 /** Get http status from incoming response. */
84 const std::string& GetStatus();
85 /** Get http statustext from incoming response. */
86 const std::string& GetStatusText();
87 /** Incoming header has been identified as a request (method url http_version\r\n). */
88 bool IsRequest();
89 /** Incoming header has been identified as a response (http_version status status_text\r\n). */
90 bool IsResponse();
91 /** Set http version to be used in outgoing request/response. */
92 void SetHttpVersion(const std::string& x);
93 /** Set http status for outgoing response. */
94 void SetStatus(const std::string& x);
95 /** Set http statustext for outgoing response. */
96 void SetStatusText(const std::string& x);
97 /** Add (and replace if exists) http header. */
98 void AddResponseHeader(const std::string& x,const std::string& y);
99 /** Add (and replace if exists) http header. */
100 void AddResponseHeader(const std::string& x,const char *format, ...);
101 /** Add http header. */
102 void AppendResponseHeader(const std::string& x,const std::string& y);
103 /** See if http header 'name' has been set. */
104 bool ResponseHeaderIsSet(const std::string& name);
105 /** Send response prepared with calls to methods SetHttpVersion, SetStatus, SetStatusText,
106 and AddResponseHeader. */
107 void SendResponse();
108 /** Send request prepared with calls to methods SetMethod, SetUrl, SetHttpVersion,
109 and AddResponseHeader. */
110 void SendRequest();
112 /** Implement this to return your own User-agent string. */
113 virtual std::string MyUseragent();
115 /** Parse url. If protocol is https, EnableSSL() will be called. */
116 void url_this(const std::string& url_in,std::string& protocol,std::string& host,port_t& port,std::string& url,std::string& file);
118 protected:
119 HTTPSocket(const HTTPSocket& s) : TcpSocket(s) {}
120 /** Reset state of socket to sucessfully implement keep-alive. */
121 virtual void Reset();
123 private:
124 HTTPSocket& operator=(const HTTPSocket& ) { return *this; }
125 bool m_first;
126 bool m_header;
127 std::string m_line;
128 std::string m_method;
129 std::string m_url;
130 std::string m_uri;
131 std::string m_query_string;
132 std::string m_http_version;
133 std::string m_status;
134 std::string m_status_text;
135 bool m_request;
136 bool m_response;
137 string_m m_response_header;
138 size_t m_body_size_left;
139 bool m_b_http_1_1;
140 bool m_b_keepalive;
141 std::list<std::pair<std::string, std::string> > m_response_header_append;
147 #ifdef SOCKETS_NAMESPACE
149 #endif
151 #endif // _SOCKETS_HTTPSocket_H