forked from solusipse/ureq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ureq.h
159 lines (125 loc) · 3.81 KB
/
ureq.h
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
https://github.com/solusipse/ureq
The MIT License (MIT)
Copyright (c) 2015 solusipse
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef UREQ_H
#define UREQ_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "ureq_pages.h"
// METHODS
#define GET "GET"
#define POST "POST"
#define ALL "ALL"
#define PUT "PUT"
#define DELETE "DELETE"
#define HTTP_V "HTTP/1.1"
// This may be redefined on your device,
// check corresponding file in hardware directory
#define MAX_REQUEST_SIZE 1024
const char *UreqMethods[] = {
GET,
POST,
ALL,
PUT,
DELETE,
NULL
};
// MIME-TYPES
struct UreqMimesList {
const char *ext;
const char *mime;
};
const struct UreqMimesList UreqMimeTypes[] = {
{"html", "text/html"},
{"htm", "text/html"},
{"js", "text/javascript"},
{"txt", "text/plain"},
{"css", "text/css"},
{"xml", "text/xml"},
{"bmp", "image/bmp"},
{"gif", "image/gif"},
{"png", "image/png"},
{"jpg", "image/jpeg"},
{"jpeg", "image/jpeg"},
{"json", "application/json"},
// Default mime-type is text/html (urls without extensions)
// Use it for files with unknown extension
{NULL, "text/html"}
};
typedef struct UreqFile {
int size;
int address;
} UreqFile;
struct Response {
int code;
char *mime;
char *header;
char *data;
};
typedef struct HttpRequest {
char *type;
char *url;
char *version;
char *message;
char *params;
char *body;
struct Response response;
int complete;
int bigFile;
int len;
UreqFile file;
// TODO: use another buffer for backend operations
// leave this one for user
char buffer[1024];
char _buffer[1024];
char *(*func)(struct HttpRequest *);
char *(*page404)(struct HttpRequest *);
int valid;
} HttpRequest;
struct Page {
char *url;
char *(*func)();
char *method;
};
#ifndef UREQ_STATIC_LIST
static struct Page *pages = NULL;
static int pageCount = 0;
#else
static struct Page pages[16];
static int pageCount = 0;
#endif
void ureq_serve(char *url, char *(func)(HttpRequest *), char *method );
HttpRequest ureq_init(char *r);
void ureq_close( struct HttpRequest *req );
void ureq_finish();
int ureq_run(struct HttpRequest *req);
static int ureq_get_header(char *h, char *r);
static int ureq_parse_header(struct HttpRequest *req, char *r);
static void ureq_remove_parameters(char *b, char *u);
static void ureq_get_query(HttpRequest *r);
static void ureq_generate_response(HttpRequest *r, char *html);
static void ureq_set_post_data(HttpRequest *r);
static char *ureq_generate_response_header(HttpRequest *r);
static char *ureq_get_code_description(int c);
static int ureq_set_error_response(HttpRequest *r);
static int ureq_set_404_response(HttpRequest *r);
#endif