-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.c
113 lines (97 loc) · 2.5 KB
/
util.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
#include <ctype.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "http_parser.h"
void gen_date_header(char* dest)
{
time_t now;
char* datestr;
time(&now);
datestr = ctime(&now);
sprintf(dest, "Date: %s", datestr);
/* drop the trailing newline generated by ctime() */
dest[strlen(dest) - 1] = '\0';
return;
}
#define _FILE_OFFSET_BITS 64
off_t fsize(const char *filename) {
struct stat st;
if (stat(filename, &st) == 0)
return st.st_size;
return -1;
}
int match_http_method(char *string)
{
char normal[16];
int i;
for(i=0; string[i] && i < 16; i++)
normal[i] = toupper(string[i]);
normal[i] = '\0';
if(!strcmp(normal, "DELETE"))
return HTTP_DELETE;
if(!strcmp(normal, "GET"))
return HTTP_GET;
if(!strcmp(normal, "HEAD"))
return HTTP_HEAD;
if(!strcmp(normal, "POST"))
return HTTP_POST;
if(!strcmp(normal, "PUT"))
return HTTP_PUT;
if(!strcmp(normal, "CONNECT"))
return HTTP_CONNECT;
if(!strcmp(normal, "OPTIONS"))
return HTTP_OPTIONS;
if(!strcmp(normal, "TRACE"))
return HTTP_TRACE;
if(!strcmp(normal, "COPY"))
return HTTP_COPY;
if(!strcmp(normal, "LOCK"))
return HTTP_LOCK;
if(!strcmp(normal, "MKCOL"))
return HTTP_MKCOL;
if(!strcmp(normal, "MOVE"))
return HTTP_MOVE;
if(!strcmp(normal, "PROPFIND"))
return HTTP_PROPFIND;
if(!strcmp(normal, "PROPPATCH"))
return HTTP_PROPPATCH;
if(!strcmp(normal, "SEARCH"))
return HTTP_SEARCH;
if(!strcmp(normal, "UNLOCK"))
return HTTP_UNLOCK;
if(!strcmp(normal, "REPORT"))
return HTTP_REPORT;
if(!strcmp(normal, "MKACTIVITY"))
return HTTP_MKACTIVITY;
if(!strcmp(normal, "CHECKOUT"))
return HTTP_CHECKOUT;
if(!strcmp(normal, "MERGE"))
return HTTP_MERGE;
if(!strcmp(normal, "M-SEARCH"))
return HTTP_MSEARCH;
if(!strcmp(normal, "NOTIFY"))
return HTTP_NOTIFY;
if(!strcmp(normal, "SUBSCRIBE"))
return HTTP_SUBSCRIBE;
if(!strcmp(normal, "UNSUBSCRIBE"))
return HTTP_UNSUBSCRIBE;
if(!strcmp(normal, "PATCH"))
return HTTP_PATCH;
if(!strcmp(normal, "PURGE"))
return HTTP_PURGE;
return -1;
}
int chomp(char *str)
{
if (!str || !*str)
return 0;
while(str[1])
++str;
if (*str!='\n')
return 0;
*str = '\0';
return '\n';
}