-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.cpp
139 lines (111 loc) · 3.45 KB
/
helpers.cpp
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
#include <stdlib.h> /* exit, atoi, malloc, free */
#include <stdio.h>
#include <unistd.h> /* read, write, close */
#include <string.h> /* memcpy, memset */
#include <sys/socket.h> /* socket, connect */
#include <netinet/in.h> /* struct sockaddr_in, struct sockaddr */
#include <netdb.h> /* struct hostent, gethostbyname */
#include <arpa/inet.h>
#include "helpers.hpp"
#include "buffer.hpp"
using namespace std;
#define HEADER_TERMINATOR "\r\n\r\n"
#define HEADER_TERMINATOR_SIZE (sizeof(HEADER_TERMINATOR) - 1)
#define CONTENT_LENGTH "Content-Length: "
#define CONTENT_LENGTH_SIZE (sizeof(CONTENT_LENGTH) - 1)
void error(const char *msg)
{
perror(msg);
exit(0);
}
void compute_message(char *message, const char *line)
{
strcat(message, line);
strcat(message, "\r\n");
}
int open_connection(char *host_ip, int portno, int ip_type, int socket_type, int flag)
{
struct sockaddr_in serv_addr;
int sockfd = socket(ip_type, socket_type, flag);
if (sockfd < 0)
error("ERROR opening socket");
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = ip_type;
serv_addr.sin_port = htons(portno);
inet_aton(host_ip, &serv_addr.sin_addr);
/* connect the socket */
if (connect(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR connecting");
return sockfd;
}
void close_connection(int sockfd)
{
close(sockfd);
}
void send_to_server(int sockfd, string message)
{
int bytes, sent = 0;
int total = message.length();
do
{
bytes = write(sockfd, (const void*)(message.c_str() + sent), total - sent);
if (bytes < 0) {
error("ERROR writing message to socket");
}
if (bytes == 0) {
break;
}
sent += bytes;
} while (sent < total);
}
string receive_from_server(int sockfd)
{
char response[BUFLEN];
buffer buffer = buffer_init();
int header_end = 0;
int content_length = 0;
do {
int bytes = read(sockfd, response, BUFLEN);
if (bytes < 0){
error("ERROR reading response from socket");
}
if (bytes == 0) {
break;
}
buffer_add(&buffer, response, (size_t) bytes);
header_end = buffer_find(&buffer, HEADER_TERMINATOR, HEADER_TERMINATOR_SIZE);
if (header_end >= 0) {
header_end += HEADER_TERMINATOR_SIZE;
int content_length_start = buffer_find_insensitive(&buffer, CONTENT_LENGTH, CONTENT_LENGTH_SIZE);
if (content_length_start < 0) {
continue;
}
content_length_start += CONTENT_LENGTH_SIZE;
content_length = strtol(buffer.data + content_length_start, NULL, 10);
break;
}
} while (1);
size_t total = content_length + (size_t) header_end;
while (buffer.size < total) {
int bytes = read(sockfd, response, BUFLEN);
if (bytes < 0) {
error("ERROR reading response from socket");
}
if (bytes == 0) {
break;
}
buffer_add(&buffer, response, (size_t) bytes);
}
buffer_add(&buffer, "", 1);
return buffer.data;
}
string basic_extract_json_response(string str)
{
if (str.find("{") != string::npos || str.find("[") != string::npos)
{
return str.substr(min(str.find_first_of("{"), str.find_first_of("[")));
}
else{
return "";
}
}