-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpRequest.h
98 lines (79 loc) · 1.87 KB
/
HttpRequest.h
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
#pragma once
#define _WINSOCK_DEPRECATED_NO_WARNINGS
// THE PLATFORM MUST BE DEFINED HERE !!! //
#define _PLATFORM_WINDOWS
// #define _PLATFORM_UNIX
// General platform independent includes //
#include <sstream>
#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <memory>
// Platform dependent includes //
#ifdef _PLATFORM_WINDOWS
#include <string>
#include <winsock.h> // winsock
#include <mmsystem.h> // windows system
#endif
#ifdef _PLATFORM_UNIX
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h> //socket
#include <arpa/inet.h> //inet_addr
#include <netinet/in.h>
#include <netdb.h> //hostent
#include <unistd.h>
#endif
// Library linking //
#ifdef _PLATFORM_WINDOWS
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "winmm.lib")
#endif
namespace httpTypes
{
enum class REQUEST_METHOD { HTTP_GET, HTTP_POST };
struct HTTP_RESPONSE
{
short statusCode;
std::string text;
};
struct REQUEST_PARAM
{
std::string name;
std::string value;
};
}
class HttpRequest
{
public:
HttpRequest(std::string, httpTypes::REQUEST_METHOD);
~HttpRequest();
void sendRequest();
void addParameter(std::string, std::string);
void removeParameter(std::string);
void removeAllParameters();
httpTypes::HTTP_RESPONSE getResponse();
private:
// Helper functions
std::vector<std::string> splitString(const std::string& s, char delimiter);
std::string constructRequestText();
// winsock handling - WINDOWS ONLY
#ifdef _PLATFORM_WINDOWS
void startWinsock();
void closeWinsock();
#endif
private:
bool m_connected;
#ifdef _PLATFORM_WINDOWS
SOCKET m_tcpSocket;
#else
int m_tcpSocket;
#endif
sockaddr_in m_serverAddr;
std::string m_url, m_host, m_requestPath;
httpTypes::REQUEST_METHOD m_requestMethod;
std::vector<httpTypes::REQUEST_PARAM> m_parameters;
std::string m_protocol;
std::string m_responseText;
};