-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsandbird.h
116 lines (97 loc) · 3.02 KB
/
sandbird.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
/**
* Copyright (c) 2016 rxi
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MIT license. See LICENSE for details.
*/
#ifndef SANDBIRD_H
#define SANDBIRD_H
#include <stddef.h>
#include <stdarg.h>
#ifdef __cplusplus
extern "C" {
#endif
#define SB_VERSION "0.1.3"
typedef struct sb_Server sb_Server;
typedef struct sb_Stream sb_Stream;
typedef struct sb_Event sb_Event;
typedef struct sb_Options sb_Options;
typedef int (*sb_Handler)(sb_Event*);
#ifdef _WIN32
typedef SOCKET sb_Socket;
#else
typedef int sb_Socket;
#define INVALID_SOCKET -1
#endif
typedef struct sb_Buffer sb_Buffer;
struct sb_Buffer { char *s; size_t len, cap; };
struct sb_Event {
int type;
void *udata;
sb_Server *server;
sb_Stream *stream;
const char *address;
const char *method;
const char *path;
};
struct sb_Options {
sb_Handler handler;
void *udata;
const char *host;
const char *port;
const char *timeout;
const char *max_lifetime;
const char *max_request_size;
};
struct sb_Stream {
int state; /* Current state of the stream */
sb_Server *server; /* The server object which owns this stream */
char address[46]; /* Remote IP address */
time_t init_time; /* Time the stream was created */
time_t last_activity; /* Time of Last I/O activity on the stream */
size_t expected_recv_len; /* Expected length of the stream's request */
size_t data_idx; /* Index of data section in recv_buf */
sb_Socket sockfd; /* Socket for this streams connection */
sb_Buffer recv_buf; /* Data received from client */
sb_Buffer send_buf; /* Data waiting to be sent to client */
FILE *send_fp; /* File currently being sent to client */
sb_Stream *next; /* Next stream in linked list */
};
enum {
SB_ESUCCESS = 0,
SB_EFAILURE = -1,
SB_EOUTOFMEM = -2,
SB_ETRUNCATED = -3,
SB_EBADSTATE = -4,
SB_EBADRESULT = -5,
SB_ECANTOPEN = -6,
SB_ENOTFOUND = -7,
SB_EFDTOOBIG = -8
};
enum {
SB_EV_CONNECT,
SB_EV_CLOSE,
SB_EV_REQUEST
};
enum {
SB_RES_OK,
SB_RES_CLOSE
};
const char *sb_error_str(int code);
sb_Server *sb_new_server(const sb_Options *opt);
void sb_close_server(sb_Server *srv);
int sb_poll_server(sb_Server *srv, int timeout);
int sb_send_status(sb_Stream *st, int code, const char *msg);
int sb_send_header(sb_Stream *st, const char *field, const char *val);
int sb_send_file(sb_Stream *st, const char *filename);
int sb_write(sb_Stream *st, const void *data, size_t len);
int sb_vwritef(sb_Stream *st, const char *fmt, va_list args);
int sb_writef(sb_Stream *st, const char *fmt, ...);
int sb_get_header(sb_Stream *st, const char *field, char *dst, size_t len);
int sb_get_var(sb_Stream *st, const char *name, char *dst, size_t len);
int sb_get_cookie(sb_Stream *st, const char *name, char *dst, size_t len);
const void *sb_get_multipart(sb_Stream *st, const char *name, size_t *len);
#ifdef __cplusplus
} // extern "C"
#endif
#endif