-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRequest.cpp
More file actions
executable file
·172 lines (141 loc) · 4.43 KB
/
Copy pathHttpRequest.cpp
File metadata and controls
executable file
·172 lines (141 loc) · 4.43 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* HttpRequest.cpp
*
* Created on: 2016年6月28日
* Author: Hery Long
*/
#include "HttpRequest.h"
HttpRequest::HttpRequest() {
// TODO Auto-generated constructor stub
this->socket_fd = 0;
memset(&this->sock_info, 0, sizeof(sock_info));
this->Request_Uri = "";
this->Request_Line = "";
this->Request_Header.clear();
this->Response = "";
//this->Response_Line = "";
//this->Response_Header.clear();
//this->Response_Body = "";
}
HttpRequest::~HttpRequest() {
// TODO Auto-generated destructor stub
close(socket_fd);
this->Request_Uri = "";
this->Request_Line = "";
this->Request_Header.clear();
this->Response = "";
//this->Response_Line = "";
//this->Response_Header.clear();
//this->Response_Body = "";
}
int HttpRequest::close_socket() {
close(socket_fd);
this->Request_Uri = "";
this->Request_Line = "";
this->Request_Header.clear();
this->Response = "";
//this->Response_Line = "";
//this->Response_Header.clear();
//this->Response_Body = "";
return 0;
}
HttpRequest& HttpRequest::connect_server(const std::string ip, const int port) {
// create socket_fd
this->socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(this->socket_fd == 0) {
std::cout<<"HttpClient: failed to create a new socket\n";
throw HttpRequestException("Failed to create a new socket.\n");
}
#ifdef __DEBUG__
std::cout<<"success to create socket.\n";
#endif
// configure the sockaddr_in
this->sock_info.sin_addr.s_addr = inet_addr(ip.c_str());
this->sock_info.sin_family = AF_INET;
this->sock_info.sin_port = htons(port);
// connect to server with ip:port
if(connect(socket_fd, (sockaddr*)&sock_info, sizeof(sock_info)) < 0) {
std::cout<<"HttpClient: failed to connect to the server with" + ip + ":" + to_string(port) + ".\n";
throw HttpRequestException("Failed to connect to the server.\n");
}
#ifdef __DEBUG__
std::cout<<"success to connect.\n";
#endif
return *this;
}
HttpRequest& HttpRequest::set_request_uri(const std::string uri) {
this->Request_Uri = uri;
return *this;
}
HttpRequest& HttpRequest::set_header(const std::string key, const std::string value) {
// if [this->Request_Header] has the key, then edit the value
// else add the new pair of [key-value]
this->Request_Header[key] = value;
return *this;
}
HttpRequest& HttpRequest::get() {
// check socket is_open();
// prepare the http message
std::string message("");
this->Request_Line = "GET " + this->Request_Uri + " HTTP/1.1\r\n";
message += this->Request_Line;
for(std::map<std::string, std::string>::iterator iter = this->Request_Header.begin();
iter != this->Request_Header.end();
iter++ ) {
message += (iter->first + ":" + iter->second + "\r\n");
}
message += "\r\n";
#ifdef __DEBUG__
std::cout<<message<<std::endl;
#endif
// send message to server
if(send(this->socket_fd, message.c_str(), strlen(message.c_str()), 0) < 0) {
std::cout<<"HttpClient: failed to send message to server.\n";
throw HttpRequestException("Failed to send message to server.\n");
}
#ifdef __DEBUG__
std::cout<<"success to send message.\n";
#endif
// receive message from server
message = "";
char buffer[BUFFER_SIZE] = { '\0' };
while(recv(this->socket_fd, buffer, BUFFER_SIZE, 0) > 0) {
message += std::string(buffer);
memset(buffer, '\0', BUFFER_SIZE);
}
this->Response = message;
return *this;
}
HttpRequest& HttpRequest::post(std::string data) {
// check socket is_open();
// prepare the http message
std::string message("");
this->Request_Line = "POST " + this->Request_Uri + " HTTP/1.1\r\n";
message += this->Request_Line;
for(std::map<std::string, std::string>::iterator iter = this->Request_Header.begin();
iter != this->Request_Header.end();
iter++ ) {
message += (iter->first + ":" + iter->second + "\r\n");
}
message += "Content-Length:" + to_string(data.length()) + "\r\n";
message += "Content-Type: application/x-www-form-urlencoded\r\n";
message += "\r\n";
message += data.c_str();
#ifdef __DEBUG__
std::cout<<message<<std::endl;
#endif
// send message to server
if(send(this->socket_fd, message.c_str(), strlen(message.c_str()), 0) < 0) {
std::cout<<"HttpClient: failed to send message to server.\n";
throw HttpRequestException("Failed to send message to server.\n");
}
// receive message from server
message = "";
char buffer[BUFFER_SIZE] = { '\0' };
while(recv(this->socket_fd, buffer, BUFFER_SIZE, 0) > 0) {
message += std::string(buffer);
memset(buffer, '\0', BUFFER_SIZE);
}
this->Response = message;
return *this;
}