summaryrefslogtreecommitdiff
path: root/src/Sockets/HttpClientSocket.h
blob: dc307a9dcf5a2a2f68e2f4d60c7b2a3cf423e175 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
 **	\file HttpClientSocket.h
 **	\date  2006-04-20
 **	\author grymse@alhem.net
**/
/*
Copyright (C) 2007  Anders Hedstrom

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#ifndef _SOCKETS_HttpClientSocket_H
#define _SOCKETS_HttpClientSocket_H

#include "sockets-config.h"
#include "HTTPSocket.h"

#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE {
#endif

/** Get http response to file or memory. 
	\ingroup http */
class HttpClientSocket : public HTTPSocket
{
public:
	HttpClientSocket(ISocketHandler&);
	HttpClientSocket(ISocketHandler&,const std::string& url_in);
	~HttpClientSocket();

	/** Parse url to protocol,host,port,url and file. */
	void Url(const std::string& url_in,std::string& host,port_t& port);

	void OnFirst();
	void OnHeader(const std::string&,const std::string&);
	void OnHeaderComplete();
	void OnData(const char *,size_t);
	void OnDelete();

	/** New callback method fires when all data is received. */
	virtual void OnContent();

	/** Write response to this file */
	void SetFilename(const std::string& );
	const std::string& Filename() const { return m_filename; }

	/** Store response in this buffer. */
	void SetDataPtr(unsigned char *,size_t);

	/** Get response headers. */
	const std::string& GetContent();

	/** Get size of response body. */
	size_t GetContentLength();

	/** Get content type from response header. */
	const std::string& GetContentType();

	/** Get size of received response body. */
	size_t GetContentPtr();
	/** Get size of received response body. */
	size_t GetPos();

	/** Complete response has been received. */
	bool Complete();

	/** Get ptr to response data buffer. */
	const unsigned char *GetDataPtr() const;

	/** Close socket when response received. */
	void SetCloseOnComplete(bool = true);

	/** Get protocol used from url. */
	const std::string& GetUrlProtocol();
	/** Get hostname from url. */
	const std::string& GetUrlHost();
	/** Get port from url. */
	port_t GetUrlPort();
	/** Get filename part of url. */
	const std::string& GetUrlFilename();

protected:
	HttpClientSocket(const HttpClientSocket& s) : HTTPSocket(s) {} // copy constructor
	HttpClientSocket& operator=(const HttpClientSocket& ) { return *this; } // assignment operator
private:
	std::string m_filename; ///< Filename to write response to
	unsigned char *m_data_ptr; ///< Ptr to buffer where to store response
	size_t m_data_size; ///< Max size of data buffer
	size_t m_content_length; ///< Content-length header received from remote
	std::string m_content; ///< Received http headers
	bool m_data_ptr_set; ///< Buffer set from outside, do not delete
	FILE *m_fil; ///< Output file
	size_t m_content_ptr; ///< Number of bytes received from body
	bool m_b_complete; ///< The entire content-length number of bytes has been received
	bool m_b_close_when_complete; ///< Close when the full response has been received
	std::string m_protocol; ///< Protocol part of url_in
	std::string m_host; ///< Hostname from url_in
	port_t m_port; ///< Port from url_in
	std::string m_url_filename; ///< Filename from url_in
	std::string m_content_type; ///< Content-type: header from response
};




#ifdef SOCKETS_NAMESPACE
} // namespace SOCKETS_NAMESPACE {
#endif
#endif // _SOCKETS_HttpClientSocket_H