-
Notifications
You must be signed in to change notification settings - Fork 0
/
host_connection.c
180 lines (159 loc) · 3.86 KB
/
host_connection.c
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
172
173
174
175
176
177
178
179
180
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define BUFLEN 256
#define MAXBUF 1024
/* Store contents of header */
void fillHeader(char header[], int sockfd){
size_t buf_i = 0;
while (buf_i < MAXBUF && 1 == read(sockfd, &header[buf_i], 1))
{
if (buf_i > 0 &&
'\n' == header[buf_i] &&
'\r' == header[buf_i - 1] &&
'\n' == header[buf_i - 2] &&
'\r' == header[buf_i - 3])
{
break;
}
buf_i++;
}
}
/* Get the length of the next chunk (chunked encoding) */
int getChunkLen(int sockfd){
char rawLen[100];
memset(rawLen, 0, 100);
size_t buf_idx = 0;
while (buf_idx < MAXBUF && 1 == read(sockfd, &rawLen[buf_idx], 1))
{
if (buf_idx > 0 &&
'\n' == rawLen[buf_idx])
{
break;
}
buf_idx++;
}
return (int)strtol(rawLen, NULL, 16);
}
int checkResponse(char* headers){
char* resp;
char respEdit[10];
int code;
resp = strstr(headers, " ");
resp = resp+1;
strncpy(respEdit, resp, 3);
printf("HTTP Server Response Code: %s\n", respEdit);
code = (int)strtol(respEdit, NULL, 10);
if (code != 200){
return 0;
}
else {
return 1;
}
}
/* Get length of the message (non-chunked encoding) */
int getConLen(char headers[]){
char * len;
len = strstr(headers, "Content-Length: ");
char lenArray[10];
memset(lenArray, 0, 10);
len = len+16;
return (int)strtol(len, NULL, 10);
}
/* Check if encoding is chunked */
int checkChunked(char headers[]){
if (strstr(headers, "Transfer-Encoding: chunked") != NULL){
return 1;
}
else {
return 0;
}
}
// Processes response from server
int getHostContent(int sockfd, char *cb, int CBUFLEN){
// Process header
char header[MAXBUF] = { 0 };
fillHeader(header, sockfd);
int respValid;
respValid = checkResponse(header);
int chunkLen = 0;
int chunkedFlag = checkChunked(header);
int contentLength;
if (chunkedFlag == 0){
contentLength = getConLen(header);
}
else {
int chunkLen = getChunkLen(sockfd);
}
// Add header to message
char * fullMessage = calloc(1, strlen(header));
strcat(fullMessage, header);
// HANDLE CHUNK DATA
if (chunkedFlag == 1){
int chunkSize = chunkLen;
while (chunkSize != 0){
// realloc handle code adapted from:
// http://stackoverflow.com/questions/27589846/dynamic-arrays-using-realloc-without-memory-leaks
void * tmp = (char *) realloc(fullMessage, (strlen(fullMessage)+chunkSize));
if (NULL == tmp)
{
printf("%s\n", "Out of memory.");
}
else
{
fullMessage = tmp;
}
int received = 0;
while (received < chunkSize-1){
char tempBuf[chunkSize];
memset(tempBuf, 0, chunkSize);
int tmp_rec = 0;
tmp_rec = recv(sockfd, tempBuf, chunkSize-received, 0);
received += tmp_rec;
strcat(fullMessage, tempBuf);
}
char nlbuf[2];
recv(sockfd, nlbuf, 2, 0);
chunkSize = getChunkLen(sockfd);
}
}
else {
// HANDLE NON-CHUNKED DATA
// realloc handle code adapted from:
// http://stackoverflow.com/questions/27589846/dynamic-arrays-using-realloc-without-memory-leaks
void * tmp = (char *) realloc(fullMessage, (strlen(fullMessage)+contentLength));
if (NULL == tmp)
{
printf("%s\n", "Out of memory.");
}
else
{
fullMessage = tmp;
}
int received = 0;
int n;
int rec = 0;
char tempBuf[256];
memset(tempBuf, 0, 256);
while ((n = recv(sockfd, tempBuf, 255, 0) > 0) || strlen(fullMessage)-strlen(header) < contentLength){
strcat(fullMessage, tempBuf);
rec += n;
memset(tempBuf, 0, 256);
}
}
if (strlen(fullMessage) > CBUFLEN){
cb = (char *) realloc(cb, strlen(fullMessage));
}
strcpy(cb, fullMessage);
free(fullMessage);
close(sockfd);
return respValid;
}