-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathechttp_parser.c
101 lines (85 loc) · 2.96 KB
/
echttp_parser.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
/* echttp - Embedded HTTP server.
*
* Copyright 2020, Pascal Martin
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* ------------------------------------------------------------------------
* A minimal HTTP server library designed for simplicity and embedding in
* existing applications.
*
* This module provides functions meant to make parsing easier.
*
* char *echttp_parser_load (const char *file);
*
* Return the whole file content as one string. The buffer is dynamically
* allocated and must be released using echttp_parser_free().
*
* Returns 0 if the file does not exist or cannot be read. In this case
* check the value of errno. If errno indicates no error, the file was
* empty.
*
* char *echttp_parser_string (const char *text);
*
* This function allocates a copy of the provided string in a way
* that is compatible with echttp_parser_free().
*
* void echttp_parser_free (char *buffer);
*
* Free the provided buffer (must have been allocated in this module).
*/
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include "echttp.h"
#include "echttp_parser.h"
// These are used for consistency checks to catch common mistakes, such as
// freeing a static buffer.
//
static char *BoundLow = 0;
static char *BoundHigh = 0;
char *echttp_parser_load (const char *file) {
struct stat filestat;
int fd;
char *buffer;
fd = open (file, O_RDONLY);
if (fd < 0) return 0;
if (fstat (fd, &filestat)) return 0;
if (filestat.st_size <= 0) return 0;
buffer = (char *) malloc(filestat.st_size+4);
if (read (fd, buffer, filestat.st_size) != filestat.st_size) return 0;
buffer[filestat.st_size] = 0;
close (fd);
if (!BoundLow || buffer < BoundLow) BoundLow = buffer;
if (!BoundHigh || buffer > BoundHigh) BoundHigh = buffer;
return buffer;
}
char *echttp_parser_string (const char *text) {
char *buffer = strdup(text);
if (!BoundLow || buffer < BoundLow) BoundLow = buffer;
if (!BoundHigh || buffer > BoundHigh) BoundHigh = buffer;
return buffer;
}
void echttp_parser_free (char *buffer) {
// Do not free an address that we cannot have allocated before.
//
if (buffer < BoundHigh && buffer > BoundLow) {
free (buffer);
}
}