-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceive.c
174 lines (156 loc) · 4.16 KB
/
receive.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <inttypes.h>
#include "receive.h"
#include "util.h"
#define BASEBUFSIZ 20
#define BASHEADSIZ 20
#define INCREASEMOD 2
#define ERRINDEX 9
static unsigned char *get_data_known(int sockfd, size_t amt, size_t *size) {
unsigned char *buffer;
size_t amtRead = 0;
ssize_t result;
buffer = safe_malloc(sizeof(unsigned char) * amt);
while ((result = read(sockfd, buffer + amtRead, amt - amtRead)) > 0) {
amtRead += result;
}
if (result < 0) {
perror_exit(NULL);
}
*size = amtRead;
return buffer;
}
static unsigned char *get_data_unkown(int sockfd, size_t *size) {
unsigned char *buffer;
size_t buffersize = BASEBUFSIZ;
size_t amtread = 0;
ssize_t result;
buffer = safe_malloc(sizeof(unsigned char) * buffersize);
while ((result = read(sockfd, buffer + amtread, buffersize - amtread)) > 0) {
amtread += result;
if (amtread != buffersize) {
continue;
}
buffersize *= INCREASEMOD;
buffer = safe_realloc(buffer, buffersize);
}
if (result < 0) {
perror_exit(NULL);
}
*size = amtread;
buffer = safe_realloc(buffer, amtread);
return buffer;
}
static int set_toggle(char letter, int *newlinetimes, int *toggle) {
if (letter == '\r') {
if ((*newlinetimes & 1) == 0) {
(*newlinetimes)++;
*toggle = 1;
return 1;
}
*newlinetimes = 0;
*toggle = 0;
return -1;
}
if (letter == '\n') {
if (*toggle == 0 || (*newlinetimes & 1) == 0) {
*newlinetimes = 0;
*toggle = 0;
return -1;
}
(*newlinetimes)++;
return 1;
}
*newlinetimes = 0;
*toggle = 0;
return 1;
}
static char *get_header(int sockfd, size_t *size) {
char *buffer;
size_t bufferSize = BASHEADSIZ;
size_t amtRead = 0;
ssize_t result;
int newlinetimes = 0, toggle = 0;
buffer = safe_malloc(sizeof(char) * bufferSize);
while ((result = read(sockfd, buffer + amtRead, 1)) > 0) {
set_toggle(buffer[amtRead], &newlinetimes, &toggle);
if (newlinetimes == 4) {
amtRead++;
break;
}
amtRead++;
if (amtRead != bufferSize) {
continue;
}
bufferSize *= INCREASEMOD;
buffer = safe_realloc(buffer, bufferSize);
}
if (result < 0) {
perror_exit(NULL);
}
buffer = safe_realloc(buffer, amtRead + 1);
buffer[amtRead] = '\0';
*size = amtRead;
return buffer;
}
static int check_content_length(char *header, size_t *len) {
const char *contentLength = "Content-Length";
int errnoSave;
char *contentLenStrPtr = strstr(header, contentLength);
char *endptr;
uintmax_t contentLen;
if (contentLenStrPtr == NULL) {
return 0;
}
errnoSave = errno;
errno = 0;
contentLen = strtoumax(contentLenStrPtr, &endptr, 10);
if (contentLenStrPtr == endptr || *endptr != '\0' || contentLen > SIZE_T_MAX) {
return 0;
}
errno = errnoSave;
return contentLen;
}
static size_t count_header_line(char *line) {
int i;
for (i = 0; line[i] != '\r' && line[i] != '\n'; ++i);
return i + 1;
}
static int check_error_code(struct receive *data) {
char *err = data->header + ERRINDEX;
size_t errLen;
if (err[0] != '4' && err[0] != '5') {
return 0;
}
errLen = count_header_line(err);
data->error = safe_malloc(sizeof(char) * errLen);
snprintf(data->error, errLen, "%s", err);
return ERRORCODE;
}
int receive(int sockfd, struct receive *data) {
size_t bodyLength, contentLength, headerLength;
data->header = get_header(sockfd, &headerLength);
data->headerLength = headerLength;
data->error = NULL;
data->body = NULL;
if (check_error_code(data) == ERRORCODE) {
return ERRORCODE;
}
if (check_content_length(data->header, &contentLength)) {
if (contentLength == 0) {
data->body = NULL;
data->bodyLength = 0;
} else {
data->body = get_data_known(sockfd, contentLength, &bodyLength);
}
} else {
data->body = get_data_unkown(sockfd, &bodyLength);
}
data->bodyLength = bodyLength;
close(sockfd);
return 0;
}