-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhpl.h
125 lines (95 loc) · 3.15 KB
/
whpl.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
/*
This software is in the public domain. Where that dedication is not recognized,
you are granted a perpetual, irrevocable license to copy and modify this file
as you see fit.
*/
#ifndef _WHPL_H_
#define _WHPL_H_
/*
Websocket/HTTP-Plug, or: whpl
- stick it where you like!
*/
#include <netinet/in.h>
struct whpl_hdr {
char* field;
char* body;
struct whpl_hdr* next;
};
struct whpl_hdr* whpl_hdr_find(struct whpl_hdr* headers, char* field);
struct whpl_req {
enum {
mUNKNOWN = 0,
mDELETE,
mGET,
mHEAD,
mPOST,
mPUT,
mCONNECT,
mOPTIONS,
mTRACE,
} method;
char* method_string;
char* raw_url;
char* version;
char* path;
char* subpath;
struct whpl_hdr* headers;
void (*on_ws_message)(struct whpl_req* req, char* msg, ssize_t len);
struct whpl_srv* srv;
struct whpl_conn* conn; /* "private" */
void* data; /* user data */
};
/* if component matches the first path component found in subpath then 1 is
* returned and subpath will point at the following component (if any).
* otherwise 0 is returned and subpath is not modified. */
int whpl_subpath_match(char* component, char** subpath);
/* various standard responses */
void whpl_status(struct whpl_req* req, int status);
void whpl_404(struct whpl_req* req);
void whpl_500(struct whpl_req* req);
/* serve static file for request. search for file `relative` in `root`
* (relative to executable). useful together with whpl_subpath_match() */
void whpl_serve_static(struct whpl_req* req, char* root, char* relative);
/* drop connection */
void whpl_kill(struct whpl_req* req);
/* accept websocket connection and register `on_message` as callback for
* websocket messages. on_message will be called with msg=NULL,len==-1 when the
* connection is dropped; you should clean up whpl_req.data if necessary when
* this happens */
void whpl_ws_accept(struct whpl_req* req, void (*on_message)(struct whpl_req* req, char* msg, ssize_t len));
/* send data through websocket */
void whpl_ws_send(struct whpl_req* req, void* data, size_t n);
void whpl_ws_send_str(struct whpl_req* req, char* str);
struct whpl_mime {
char* extension;
char* mime;
struct whpl_mime* next;
};
struct whpl_srv {
/* bind address for http/websocket */
struct sockaddr_in http_ws_addr;
/* callbacks. may be NULL */
void (*on_http_request)(struct whpl_req* req);
void (*on_ws_connect)(struct whpl_req* req);
struct whpl_mime* mimes;
void* data;
};
/* initialize whpl server */
void whpl_srv_init(struct whpl_srv* srv);
/* configure to listen on any address */
void whpl_srv_set_inaddr_any(struct whpl_srv* srv);
/* set server port */
void whpl_srv_set_port(struct whpl_srv* srv, short port);
/* register mimetype for extension; used by whpl_serve_static() */
void whpl_srv_add_mime(struct whpl_srv* srv, char* extension, char* mimetype);
/* find mime for extension */
struct whpl_mime* whpl_srv_find_mime(struct whpl_srv* srv, char* extension);
/* run server */
void whpl_run(struct whpl_srv* srv);
/* case insensitive string compare */
int whpl_ieq(char* a, char* b);
/* get substring of path which is the extension, empty string if not found */
char* whpl_ext(char* path);
/* path of exectuable */
char* whpl_exepath();
#endif /*_WHPL_H_*/