-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_parser.c
More file actions
112 lines (94 loc) · 2.41 KB
/
http_parser.c
File metadata and controls
112 lines (94 loc) · 2.41 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
// File: http_parser.c
// Created May 27, 2014
// Michael Baptist - mbaptist@ucsc.edu
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <dirent.h>
#include <sys/stat.h>
// comment this out to turn on debug prints.
//#define NDEBUG NDEBUG
#include "http_parser.h"
#include "utils.h"
#define SUCCESS 0
#define FAILURE 1
// counts new lines in an http message
size_t count_newlines(char buffer[], size_t bufferlen) {
size_t c = 0;
for (size_t i = 0; i < bufferlen; i++) {
if (buffer[i] == '\n') c++;
}
return c;
}
void parse_http_header(char *lines[], char *buf, size_t numlines) {
// get server ip and port from server list.
size_t length = strlen(buf);
char *copy = malloc(length + 1);
// string copy.
size_t i = 0;
for (i = 0; i < (length + 1) && buf[i] != '\0'; i++) {
copy[i] = buf[i];
}
for ( ; i < (length + 1); i++) {
copy[i] = '\0';
}
// remove all '\r' chars
size_t x = 0;
for (x = 0; x < (length + 1) && copy[x] != '\0'; x++) {
if (copy[x] == '\r') {
copy[x] = '\n';
}
}
char *tok = copy;
i = 0;
while ((tok = strtok(tok, "\n")) != NULL) {
size_t len = strlen(tok);
lines[i] = calloc(1, len + 1);
bcopy(tok, lines[i], len + 1);
lines[i][len] = '\0';
tok = NULL;
if (i < numlines) {
++i;
} else {
break;
}
}
free(copy);
}
void parse_http_header_line(char *pieces[], char *line, size_t pieceslen) {
// get server ip and port from server list.
size_t length = strlen(line);
char *copy = malloc(length + 1);
// string copy.
size_t i = 0;
for (i = 0; i < (length + 1) && line[i] != '\0'; i++) {
copy[i] = line[i];
}
for ( ; i < (length + 1); i++) {
copy[i] = '\0';
}
char *tok = copy;
i = 0;
while ((tok = strtok(tok, " ")) != NULL) {
size_t len = strlen(tok);
pieces[i] = calloc(1, len + 1);
bcopy(tok, pieces[i], len + 1);
tok = NULL;
if (i < pieceslen) {
++i;
} else {
break;
}
}
free(copy);
}
void free_parse_allocs(char *allocations[], int len) {
for (int i = 0; i < len; ++i) {
if (allocations[i] != NULL) {
free(allocations[i]);
}
}
}