-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp_server.h
More file actions
152 lines (133 loc) · 3.71 KB
/
Copy pathhttp_server.h
File metadata and controls
152 lines (133 loc) · 3.71 KB
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
/**
* @Project [HTTP Module]
* @File [http_server.h]
* @Author [caydyn@icloud.com]
* @Version [1.0]
*/
#ifndef SERVER_H
#define SERVER_H
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <microhttpd.h> //gcc -lmicrohttpt
#define DEBUG 1
#define PORT_NUMBER 80
#define MAX_API sizeof(uint8_t)
#define MAX_FILE_CUSTOM_PATH sizeof(uint8_t)
#define FILE_PATH_LEN_MAX 128
#define FILE_EXTEN_LEN_MAX 8
#define ENABLE_AUTHENTICATE 0
#define AUTHENTICATE_LOGIN_URL "/login"
#define AUTHENTICATE_COOKIE_FORAMT "%s=%llu;Max-Age=%d"
#define AUTHENTICATE_COOKIE_NAME "Default_Cookie"
#define AUTHENTICATE_COOKIE_TIME "360000"
#define LOGIN_AUTHENTICATE_PASSWD "ADMIN"
#define LOGIN_HTML_PATH ""
uint64_t last_cookie = 0;
struct MHD_Daemon *_http_daemon;
struct MHD_Connection * _cur_connect;
static char WEB_PATH[128];
#define true 1
#define false 0
/*gui file type */
#define HTM_FILE 1
#define JAS_FILE 2
#define CSS_FILE 3
#define PNG_FILE 4
#define JPG_FILE 5
#define XML_FILE 6
#define CSV_FILE 7
#define PDF_FILE 8
#define APP_FILE 9
#define JSO_FILE 10
#define TXT_FILE 11
#define WOF_FILE 12
#define TTF_FILE 13
#define EOT_FILE 14
#define OTF_FILE 15
#define SVG_FILE 16
#define MP3_FILE 17
#define WAV_FILE 18
#define GIF_FILE 19
#define MP4_FILE 20
#define AVI_FILE 21
#define FLV_FILE 22
const static char *mime_type_string[] = {
"placeholder_not_used",
"text/html",
"text/css",
"application/x-javascript",
"image/png",
"image/jpeg",
"text/xml",
"application/csv",
"application/pdf",
"application/x-msdownload",
"application/json",
"application/text",
"application/font-woff",
"application/font-ttf",
"application/vnd.ms-fontobject",
"application/ont-otf",
"image/svg+xml",
"audio/mpeg3",
"audio/wav",
"image/gif",
"audio/mp4",
"video/x-msvideo",
"video/x-flv",
};
#define UPLOAD_SYMBLE "\r\n"
#define UPLOAD_SYMBLE_NB 3
#define FILE_SIZE_FLAG "name"
#define DOUBLE_QUOTES 34
#define SIZE_OFFSET 5
#define NAME_OFFSET 9
const static char * _200_page = "<html><head><title>200 Success</title></head><body> \
<h1>200, Request successfully processed</h1></body></html>";
const static char * _302_page = "<html><head><title>302 Redirect</title></head><body> \
<h1>302, Redirect request</h1></body></html>";
const static char * _404_page = "<html><head><title>404 Error</title></head><body> \
<h1>404, Request not found</h1></body></html>";
const static char * _500_page = "<html><head><title>500 Error</title></head><body> \
<h1>500, Request API Not Found or Handle Error: [%d]</h1></body></html>";
typedef struct {
char url[512];
char method[10];
uint32_t offset;
uint32_t post_data_len;
char *post_data;
/*file upload*/
char file_name[64];
uint8_t after;
uint32_t used;
uint32_t file_size;
} next_connection_info;
/*
HOW TO DEFINE THIS CALLBACK FUNCTION !!
data: geted post request data (sometime is NULL!)
size: post data size
response_data: custom response data buffer
enable_redirect: enable http 302 action
redirect_url: new url string
*/
typedef int (*APIFunc) (const char *data, const uint32_t size,
char **response_data, uint8_t *enable_redirect, char *redirect_url);
APIFunc APIFA[MAX_API];
#define REGISTER_API(ID, FUNC) APIFA[ID] = FUNC;
static uint8_t custom_file_path_nb;
char CUSTOM_FILE_EXTEN[MAX_FILE_CUSTOM_PATH][FILE_EXTEN_LEN_MAX];
char CUSTOM_FILE_PATH[MAX_FILE_CUSTOM_PATH][FILE_PATH_LEN_MAX];
uint8_t init_http_server(char *);
void release_http_server(void);
int register_api_handle(uint32_t, APIFunc);
int register_custom_file_path(const char*, const char*);
inline const char *get_url_param(char *);
#endif