-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttputils.c
141 lines (110 loc) · 2.22 KB
/
httputils.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
#include "httputils.h"
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <ctype.h>
void string_get_file_extension(const char *str, char* buffer, size_t buffer_len)
{
// Initialize the output as empty string.
*buffer = 0;
size_t len = strlen(str);
// If the length of the filename is 0, it has no extension.
if (len == 0) {
return;
}
for (int64_t i = len - 1; i >= 0; --i) {
char c = str[i];
// Could not find extension or the extension is too long!
if (len - i >= buffer_len) {
return;
}
// Extension must be valid alphanumeric characters.
if (!isalnum(c) && c != '.') {
return;
}
// Found the beginning of the extension, copy it into the buffer.
if (c == '.') {
strcpy(buffer, &str[i]);
return;
}
}
}
static char *string_remove_leading_white_space(char* str)
{
if (str == NULL) {
return NULL;
}
for (;;) {
switch (*str) {
case ' ':
case '\n':
case '\r':
case '\t':
*str++ = '\0';
continue;
case '\0':
return NULL;
}
break;
}
return str;
}
static char *string_skip_non_white_space(char *str, bool skip_until_new_line)
{
if (str == NULL) {
return NULL;
}
for (;;) {
switch (*str) {
case ' ':
case '\t':
if (!skip_until_new_line) {
return str;
}
break;
case '\n':
case '\r':
return str;
case '\0':
return NULL;
}
++str;
}
return str;
}
static char *string_skip_past_line_break(char* str)
{
if (str == NULL) {
return NULL;
}
for (;;) {
switch (*str) {
case '\0':
return NULL;
case '\r':
case '\n':
++str;
if (*str == '\n' || *str == '\r') {
++str;
}
return str;
}
++str;
}
return str;
}
char *string_parse_header_text(char *str, char **header, char **value)
{
// Remove all leading white space. The starting string is the header.
str = string_remove_leading_white_space(str);
*header = str;
// Skip the header text and possible white space after it, which gets to the header value.
str = string_skip_non_white_space(str, false);
str = string_remove_leading_white_space(str);
*value = str;
// Skip the header value and terminate the string.
str = string_skip_non_white_space(str, true);
str = string_skip_past_line_break(str);
return str;
}