summaryrefslogtreecommitdiff
path: root/src/modules/httpclient.h
blob: 8e1a314dd0af4342e9ab38326c4215f778ac25dc (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
#include "base.h"

#ifndef HTTPCLIENT_H__
#define HTTPCLIENT_H__

#include <string>
#include <map>

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

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

	const std::string &GetURL()
	{
		return url;
	}
	
	Module *GetSrc()
	{
		return src;
	}

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

	HeaderMap GetHeaders()
	{
		return Headers;
	}
	
	void SendRequest()
	{
		Module *HTTPModule = Instance->FindModule("m_http_client.so");
		if (!HTTPModule)
		{
			Instance->Log(DEFAULT, "HTTP module not loaded!");
			return;
		}
		
		Request req((char *)this, src, HTTPModule);
		req.Send();
	}
};

class HTTPClientResponse : public classbase
{
 protected:
	friend class HTTPSocket;
	
	std::string url;
	std::string data;
	int response;
	std::string responsestr;
	HeaderMap Headers;
 public:
	HTTPClientResponse(std::string &url, int response, std::string responsestr)
		: url(url), response(response), responsestr(responsestr)
	{
	}
	
	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