minor changes
[anytun.git] / Sockets / HttpClientSocket.h
blobdc307a9dcf5a2a2f68e2f4d60c7b2a3cf423e175
1 /**
2 ** \file HttpClientSocket.h
3 ** \date 2006-04-20
4 ** \author grymse@alhem.net
5 **/
6 /*
7 Copyright (C) 2007 Anders Hedstrom
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #ifndef _SOCKETS_HttpClientSocket_H
24 #define _SOCKETS_HttpClientSocket_H
26 #include "sockets-config.h"
27 #include "HTTPSocket.h"
29 #ifdef SOCKETS_NAMESPACE
30 namespace SOCKETS_NAMESPACE {
31 #endif
33 /** Get http response to file or memory.
34 \ingroup http */
35 class HttpClientSocket : public HTTPSocket
37 public:
38 HttpClientSocket(ISocketHandler&);
39 HttpClientSocket(ISocketHandler&,const std::string& url_in);
40 ~HttpClientSocket();
42 /** Parse url to protocol,host,port,url and file. */
43 void Url(const std::string& url_in,std::string& host,port_t& port);
45 void OnFirst();
46 void OnHeader(const std::string&,const std::string&);
47 void OnHeaderComplete();
48 void OnData(const char *,size_t);
49 void OnDelete();
51 /** New callback method fires when all data is received. */
52 virtual void OnContent();
54 /** Write response to this file */
55 void SetFilename(const std::string& );
56 const std::string& Filename() const { return m_filename; }
58 /** Store response in this buffer. */
59 void SetDataPtr(unsigned char *,size_t);
61 /** Get response headers. */
62 const std::string& GetContent();
64 /** Get size of response body. */
65 size_t GetContentLength();
67 /** Get content type from response header. */
68 const std::string& GetContentType();
70 /** Get size of received response body. */
71 size_t GetContentPtr();
72 /** Get size of received response body. */
73 size_t GetPos();
75 /** Complete response has been received. */
76 bool Complete();
78 /** Get ptr to response data buffer. */
79 const unsigned char *GetDataPtr() const;
81 /** Close socket when response received. */
82 void SetCloseOnComplete(bool = true);
84 /** Get protocol used from url. */
85 const std::string& GetUrlProtocol();
86 /** Get hostname from url. */
87 const std::string& GetUrlHost();
88 /** Get port from url. */
89 port_t GetUrlPort();
90 /** Get filename part of url. */
91 const std::string& GetUrlFilename();
93 protected:
94 HttpClientSocket(const HttpClientSocket& s) : HTTPSocket(s) {} // copy constructor
95 HttpClientSocket& operator=(const HttpClientSocket& ) { return *this; } // assignment operator
96 private:
97 std::string m_filename; ///< Filename to write response to
98 unsigned char *m_data_ptr; ///< Ptr to buffer where to store response
99 size_t m_data_size; ///< Max size of data buffer
100 size_t m_content_length; ///< Content-length header received from remote
101 std::string m_content; ///< Received http headers
102 bool m_data_ptr_set; ///< Buffer set from outside, do not delete
103 FILE *m_fil; ///< Output file
104 size_t m_content_ptr; ///< Number of bytes received from body
105 bool m_b_complete; ///< The entire content-length number of bytes has been received
106 bool m_b_close_when_complete; ///< Close when the full response has been received
107 std::string m_protocol; ///< Protocol part of url_in
108 std::string m_host; ///< Hostname from url_in
109 port_t m_port; ///< Port from url_in
110 std::string m_url_filename; ///< Filename from url_in
111 std::string m_content_type; ///< Content-type: header from response
117 #ifdef SOCKETS_NAMESPACE
118 } // namespace SOCKETS_NAMESPACE {
119 #endif
120 #endif // _SOCKETS_HttpClientSocket_H