summaryrefslogtreecommitdiff
path: root/src/modules/httpclient.h
blob: c5e84261f770b30e54e5ff80cc0e743761113f29 (plain)
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
122
123
124
125
126
127
/*       +------------------------------------+
 *       | Inspire Internet Relay Chat Daemon |
 *       +------------------------------------+
 *
 *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
 * See: http://www.inspircd.org/wiki/index.php/Credits
 *
 * This program is free but copyrighted software; see
 *            the file COPYING for details.
 *
 * ---------------------------------------------------
 */

#include "base.h"

#ifndef HTTPCLIENT_H__
#define HTTPCLIENT_H__

#include <string>
#include <map>

typedef std::map<std::string,std::string> HeaderMap;

const char* HTTP_CLIENT_RESPONSE = "HTTPCLIENT_RESPONSE";
const char* HTTP_CLIENT_REQUEST = "HTTPCLIENT_REQUEST";

/** This class represents an outgoing HTTP request
 */
class HTTPClientRequest : public Request
{
 protected:
	std::string url;
	InspIRCd *Instance;
	Module *src;
	HeaderMap Headers;
 public:
	HTTPClientRequest(InspIRCd *Instance, Module *src, Module* target, const std::string &url)
		: Request(src, target, HTTP_CLIENT_REQUEST), url(url), Instance(Instance), src(src)
	{
		Headers["User-Agent"] = "InspIRCd (m_http_client.so)";
		Headers["Connection"] = "Close";
		Headers["Accept"] = "*/*";
	}

	HTTPClientRequest() : Request(NULL, NULL, HTTP_CLIENT_REQUEST)
	{
	}

	const std::string &GetURL()
	{
		return url;
	}

	void AddHeader(std::string &header, std::string &data)
	{
		Headers[header] = data;
	}
	
	void DeleteHeader(std::string &header)
	{
		Headers.erase(header);
	}

	HeaderMap GetHeaders()
	{
		return Headers;
	}
};

class HTTPClientResponse : public Request
{
 protected:
	friend class HTTPSocket;
	
	std::string url;
	std::string data;
	int response;
	std::string responsestr;
	HeaderMap Headers;
 public:
	HTTPClientResponse(Module* src, Module* target, std::string &url, int response, std::string responsestr)
		: Request(src, target, HTTP_CLIENT_RESPONSE), url(url), response(response), responsestr(responsestr)
	{
	}

	HTTPClientResponse() : Request(NULL, NULL, HTTP_CLIENT_RESPONSE)
	{
	}

	void SetData(const std::string &ndata)
	{
		data = ndata;
	}
	
	void AddHeader(const std::string &header, const std::string &data)
	{
		Headers[header] = data;
	}
	
	const std::string &GetURL()
	{
		return url;
	}
	
	const std::string &GetData()
	{
		return data;
	}
	
	int GetResponse(std::string &str)
	{
		str = responsestr;
		return response;
	}
	
	std::string GetHeader(const std::string &header)
	{
		HeaderMap::iterator i = Headers.find(header);
		
		if (i != Headers.end())
			return i->second;
		else
			return "";
	}
};

#endif