-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_request.c
103 lines (91 loc) · 2.5 KB
/
parse_request.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
/*
* Author:: Matt Chapman ([email protected])
*
* Copyright (C) 2018 Matt Chapman
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include <stdlib.h>
#include <string.h>
#include "parse_request.h"
int parse_request(const char* request, struct parsed_request** req)
{
char* in;
char* end;
char* div;
*req = calloc(1, sizeof(struct parsed_request));
in = (*req)->method = strdup(request);
end = in + strlen(in);
// reset everything to empty string:
(*req)->url_protocol = end;
(*req)->hostname = end;
(*req)->port = -1;
(*req)->path = end;
(*req)->args = end;
(*req)->protocol = end;
// this slices off method from the front.
div = strchr(in, ' ');
if (div == NULL) return 0;
div[0] = 0;
in=div+1;
if (in >= end) return 0;
// pop the end off, (HTTP/1.1)
div = strrchr(in, ' ');
if (div == NULL) return 0;
div[0] = 0;
(*req)->protocol = div+1;
end = div;
if (in >= end) return 0;
// now we have the middle [in, end]. proto://hostname:port/path/stuff?args=values
div = strchr(in, ':');
if (div == NULL) return 0;
div[0] = 0;
(*req)->url_protocol = in;
in = div + 3 ; // skip '://'
if (in >= end) return 0;
// hostname(:port)?
(*req)->hostname = in;
div = strchr(in, '/');
if (div != NULL) {
div[0] = 0;
in = div+1;
} else {
in = end;
}
div = strrchr((*req)->hostname, ':');
if (div == NULL) {
(*req)->port = -1;
} else {
div[0] = 0;
(*req)->port = atoi(div+1);
}
if (in >= end) return 0;
// get the path, (everything between host and ?)
div = strchr(in, '?');
if (div != NULL) {
div[0] = 0;
}
(*req)->path = in;
if (div != NULL) {
// whatever is left is the args
(*req)->args = div+1;
}
return 0;
}
int free_request(struct parsed_request* req)
{
free(req->method);
free(req);
return 0;
}