agoo 2.5.5 → 2.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of agoo might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +1 -1
- data/ext/agoo/agoo.c +4 -3
- data/ext/agoo/atomic.h +120 -0
- data/ext/agoo/bind.c +52 -52
- data/ext/agoo/bind.h +13 -13
- data/ext/agoo/con.c +499 -481
- data/ext/agoo/con.h +47 -39
- data/ext/agoo/debug.c +42 -42
- data/ext/agoo/debug.h +1 -1
- data/ext/agoo/doc.c +17 -17
- data/ext/agoo/doc.h +12 -12
- data/ext/agoo/err.c +18 -18
- data/ext/agoo/err.h +27 -27
- data/ext/agoo/error_stream.c +9 -9
- data/ext/agoo/extconf.rb +3 -0
- data/ext/agoo/gqlintro.c +43 -43
- data/ext/agoo/gqlintro.h +1 -1
- data/ext/agoo/gqlvalue.c +131 -131
- data/ext/agoo/gqlvalue.h +32 -32
- data/ext/agoo/graphql.c +158 -158
- data/ext/agoo/graphql.h +34 -33
- data/ext/agoo/hook.c +15 -14
- data/ext/agoo/hook.h +18 -14
- data/ext/agoo/http.c +14 -14
- data/ext/agoo/http.h +4 -4
- data/ext/agoo/kinds.h +5 -5
- data/ext/agoo/log.c +232 -224
- data/ext/agoo/log.h +93 -93
- data/ext/agoo/method.h +17 -17
- data/ext/agoo/page.c +88 -86
- data/ext/agoo/page.h +21 -21
- data/ext/agoo/pub.c +36 -36
- data/ext/agoo/pub.h +23 -23
- data/ext/agoo/queue.c +37 -38
- data/ext/agoo/queue.h +20 -19
- data/ext/agoo/rack_logger.c +13 -13
- data/ext/agoo/ready.c +357 -0
- data/ext/agoo/ready.h +41 -0
- data/ext/agoo/req.c +11 -11
- data/ext/agoo/req.h +30 -31
- data/ext/agoo/request.c +46 -46
- data/ext/agoo/request.h +2 -2
- data/ext/agoo/res.c +40 -18
- data/ext/agoo/res.h +14 -14
- data/ext/agoo/response.c +6 -6
- data/ext/agoo/response.h +9 -9
- data/ext/agoo/rhook.c +3 -3
- data/ext/agoo/rhook.h +1 -1
- data/ext/agoo/rlog.c +47 -42
- data/ext/agoo/rlog.h +0 -1
- data/ext/agoo/rresponse.c +33 -33
- data/ext/agoo/rresponse.h +1 -1
- data/ext/agoo/rserver.c +184 -175
- data/ext/agoo/rserver.h +2 -2
- data/ext/agoo/rupgraded.c +41 -41
- data/ext/agoo/rupgraded.h +3 -3
- data/ext/agoo/sdl.c +80 -80
- data/ext/agoo/sdl.h +1 -1
- data/ext/agoo/seg.h +2 -2
- data/ext/agoo/server.c +143 -117
- data/ext/agoo/server.h +43 -42
- data/ext/agoo/sse.c +7 -7
- data/ext/agoo/sse.h +4 -4
- data/ext/agoo/subject.c +5 -5
- data/ext/agoo/subject.h +6 -6
- data/ext/agoo/text.c +21 -21
- data/ext/agoo/text.h +14 -13
- data/ext/agoo/upgraded.c +41 -40
- data/ext/agoo/upgraded.h +41 -40
- data/ext/agoo/websocket.c +42 -42
- data/ext/agoo/websocket.h +16 -16
- data/lib/agoo/version.rb +1 -1
- data/test/static_test.rb +2 -0
- metadata +5 -5
- data/ext/agoo/log_queue.h +0 -30
- data/ext/agoo/sub.c +0 -111
- data/ext/agoo/sub.h +0 -36
data/ext/agoo/log.h
CHANGED
@@ -5,124 +5,124 @@
|
|
5
5
|
|
6
6
|
#include <pthread.h>
|
7
7
|
#include <stdarg.h>
|
8
|
-
#include <stdatomic.h>
|
9
8
|
#include <stdbool.h>
|
10
9
|
#include <stdint.h>
|
11
10
|
#include <stdio.h>
|
12
11
|
|
12
|
+
#include "atomic.h"
|
13
13
|
#include "err.h"
|
14
14
|
|
15
15
|
typedef enum {
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
}
|
23
|
-
|
24
|
-
typedef struct
|
16
|
+
AGOO_FATAL = 0,
|
17
|
+
AGOO_ERROR = 1,
|
18
|
+
AGOO_WARN = 2,
|
19
|
+
AGOO_INFO = 3,
|
20
|
+
AGOO_DEBUG = 4,
|
21
|
+
AGOO_UNKNOWN = 5,
|
22
|
+
} agooLogLevel;
|
23
|
+
|
24
|
+
typedef struct _agooColor {
|
25
25
|
const char *name;
|
26
26
|
const char *ansi;
|
27
|
-
} *
|
28
|
-
|
29
|
-
#define
|
30
|
-
#define
|
31
|
-
#define
|
32
|
-
#define
|
33
|
-
#define
|
34
|
-
#define
|
35
|
-
#define
|
36
|
-
#define
|
37
|
-
#define
|
38
|
-
#define
|
39
|
-
#define
|
40
|
-
#define
|
41
|
-
#define
|
42
|
-
#define
|
43
|
-
#define
|
44
|
-
|
45
|
-
typedef struct
|
46
|
-
|
47
|
-
typedef struct
|
48
|
-
struct
|
27
|
+
} *agooColor;
|
28
|
+
|
29
|
+
#define AGOO_BLACK "black"
|
30
|
+
#define AGOO_RED "red"
|
31
|
+
#define AGOO_GREEN "green"
|
32
|
+
#define AGOO_YELLOW "yellow"
|
33
|
+
#define AGOO_BLUE "blue"
|
34
|
+
#define AGOO_MAGENTA "magenta"
|
35
|
+
#define AGOO_CYAN "cyan"
|
36
|
+
#define AGOO_WHITE "white"
|
37
|
+
#define AGOO_GRAY "gray"
|
38
|
+
#define AGOO_DARK_RED "dark_red"
|
39
|
+
#define AGOO_DARK_GREEN "dark_green"
|
40
|
+
#define AGOO_BROWN "brown"
|
41
|
+
#define AGOO_DARK_BLUE "dark_blue"
|
42
|
+
#define AGOO_PURPLE "purple"
|
43
|
+
#define AGOO_DARK_CYAN "dark_cyan"
|
44
|
+
|
45
|
+
typedef struct _agooLog *agooLog;
|
46
|
+
|
47
|
+
typedef struct _agooLogCat {
|
48
|
+
struct _agooLogCat *next;
|
49
49
|
char label[32];
|
50
|
-
|
50
|
+
agooColor color;
|
51
51
|
int level;
|
52
52
|
bool on;
|
53
|
-
} *
|
53
|
+
} *agooLogCat;
|
54
54
|
|
55
|
-
typedef struct
|
56
|
-
|
55
|
+
typedef struct _agooLogEntry {
|
56
|
+
agooLogCat cat;
|
57
57
|
int64_t when; // nano UTC
|
58
58
|
char *whatp;
|
59
59
|
char what[104];
|
60
60
|
volatile bool ready;
|
61
61
|
char *tidp;
|
62
62
|
char tid[40];
|
63
|
-
} *
|
63
|
+
} *agooLogEntry;
|
64
64
|
|
65
|
-
struct
|
66
|
-
|
67
|
-
char
|
68
|
-
char
|
69
|
-
FILE
|
70
|
-
int
|
71
|
-
int
|
72
|
-
long
|
73
|
-
pthread_t
|
74
|
-
volatile bool
|
75
|
-
bool
|
76
|
-
bool
|
77
|
-
bool
|
78
|
-
bool
|
79
|
-
int
|
80
|
-
int64_t
|
81
|
-
int64_t
|
82
|
-
char
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
_Atomic(
|
87
|
-
_Atomic(
|
88
|
-
atomic_flag
|
89
|
-
atomic_int
|
90
|
-
int
|
91
|
-
int
|
92
|
-
|
93
|
-
void
|
65
|
+
struct _agooLog {
|
66
|
+
agooLogCat cats;
|
67
|
+
char dir[1024];
|
68
|
+
char app[16];
|
69
|
+
FILE *file; // current output file
|
70
|
+
int max_files;
|
71
|
+
int max_size;
|
72
|
+
long size; // current file size
|
73
|
+
pthread_t thread;
|
74
|
+
volatile bool done;
|
75
|
+
bool console; // if true print log message to stdout
|
76
|
+
bool classic; // classic in stdout
|
77
|
+
bool colorize; // color in stdout
|
78
|
+
bool with_pid;
|
79
|
+
int zone; // timezone offset from GMT in seconds
|
80
|
+
int64_t day_start;
|
81
|
+
int64_t day_end;
|
82
|
+
char day_buf[16];
|
83
|
+
|
84
|
+
agooLogEntry q;
|
85
|
+
agooLogEntry end;
|
86
|
+
_Atomic(agooLogEntry) head;
|
87
|
+
_Atomic(agooLogEntry) tail;
|
88
|
+
atomic_flag push_lock;
|
89
|
+
atomic_int wait_state;
|
90
|
+
int rsock;
|
91
|
+
int wsock;
|
92
|
+
|
93
|
+
void (*on_error)(agooErr err);
|
94
94
|
};
|
95
95
|
|
96
|
-
extern struct
|
97
|
-
extern struct
|
98
|
-
extern struct
|
99
|
-
extern struct
|
100
|
-
extern struct
|
101
|
-
extern struct
|
102
|
-
extern struct
|
103
|
-
extern struct
|
104
|
-
extern struct
|
105
|
-
extern struct
|
106
|
-
extern struct
|
107
|
-
|
108
|
-
extern void
|
109
|
-
extern void
|
110
|
-
|
111
|
-
extern void
|
112
|
-
extern bool
|
113
|
-
extern void
|
114
|
-
|
115
|
-
extern void
|
116
|
-
extern void
|
117
|
-
extern
|
96
|
+
extern struct _agooLog agoo_log;
|
97
|
+
extern struct _agooLogCat agoo_fatal_cat;
|
98
|
+
extern struct _agooLogCat agoo_error_cat;
|
99
|
+
extern struct _agooLogCat agoo_warn_cat;
|
100
|
+
extern struct _agooLogCat agoo_info_cat;
|
101
|
+
extern struct _agooLogCat agoo_debug_cat;
|
102
|
+
extern struct _agooLogCat agoo_con_cat;
|
103
|
+
extern struct _agooLogCat agoo_req_cat;
|
104
|
+
extern struct _agooLogCat agoo_resp_cat;
|
105
|
+
extern struct _agooLogCat agoo_eval_cat;
|
106
|
+
extern struct _agooLogCat agoo_push_cat;
|
107
|
+
|
108
|
+
extern void agoo_log_init(const char *app);
|
109
|
+
extern void agoo_log_open_file();
|
110
|
+
|
111
|
+
extern void agoo_log_close();
|
112
|
+
extern bool agoo_log_flush(double timeout);
|
113
|
+
extern void agoo_log_rotate();
|
114
|
+
|
115
|
+
extern void agoo_log_cat_reg(agooLogCat cat, const char *label, agooLogLevel level, const char *color, bool on);
|
116
|
+
extern void agoo_log_cat_on(const char *label, bool on);
|
117
|
+
extern agooLogCat agoo_log_cat_find(const char *label);
|
118
118
|
|
119
119
|
// Function to call to make a log entry.
|
120
|
-
extern void
|
121
|
-
extern void
|
122
|
-
extern void
|
120
|
+
extern void agoo_log_cat(agooLogCat cat, const char *fmt, ...);
|
121
|
+
extern void agoo_log_tid_cat(agooLogCat cat, const char *tid, const char *fmt, ...);
|
122
|
+
extern void agoo_log_catv(agooLogCat cat, const char *tid, const char *fmt, va_list ap);
|
123
123
|
|
124
|
-
extern
|
124
|
+
extern int agoo_log_start(agooErr err, bool with_pid);
|
125
125
|
|
126
|
-
extern
|
126
|
+
extern agooColor find_color(const char *name);
|
127
127
|
|
128
128
|
#endif /* AGOO_LOG_H */
|
data/ext/agoo/method.h
CHANGED
@@ -4,23 +4,23 @@
|
|
4
4
|
#define AGOO_METHOD_H
|
5
5
|
|
6
6
|
typedef enum {
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
7
|
+
AGOO_CONNECT = 'C',
|
8
|
+
AGOO_DELETE = 'D',
|
9
|
+
AGOO_GET = 'G',
|
10
|
+
AGOO_HEAD = 'H',
|
11
|
+
AGOO_OPTIONS = 'O',
|
12
|
+
AGOO_POST = 'P',
|
13
|
+
AGOO_PUT = 'U',
|
14
|
+
AGOO_PATCH = 'T',
|
15
|
+
AGOO_ALL = 'A',
|
16
|
+
AGOO_NONE = '\0',
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
}
|
18
|
+
AGOO_ON_MSG = 'M', // use for on_message callback
|
19
|
+
AGOO_ON_BIN = 'B', // use for on_message callback with binary (ASCII8BIT)
|
20
|
+
AGOO_ON_CLOSE = 'X', // use for on_close callback
|
21
|
+
AGOO_ON_SHUTDOWN = 'S', // use for on_shotdown callback
|
22
|
+
AGOO_ON_EMPTY = 'E', // use for on_drained callback
|
23
|
+
AGOO_ON_ERROR = 'F', // use for on_error callback
|
24
|
+
} agooMethod;
|
25
25
|
|
26
26
|
#endif // AGOO_METHOD_H
|
data/ext/agoo/page.c
CHANGED
@@ -21,36 +21,36 @@
|
|
21
21
|
#define MIME_BUCKET_SIZE 64
|
22
22
|
#define MIME_BUCKET_MASK 63
|
23
23
|
|
24
|
-
typedef struct
|
25
|
-
struct
|
24
|
+
typedef struct _slot {
|
25
|
+
struct _slot *next;
|
26
26
|
char key[MAX_KEY_LEN + 1];
|
27
|
-
|
27
|
+
agooPage value;
|
28
28
|
uint64_t hash;
|
29
29
|
int klen;
|
30
30
|
} *Slot;
|
31
31
|
|
32
|
-
typedef struct
|
33
|
-
struct
|
32
|
+
typedef struct _mimeSlot {
|
33
|
+
struct _mimeSlot *next;
|
34
34
|
char key[MAX_MIME_KEY_LEN + 1];
|
35
35
|
char *value;
|
36
36
|
uint64_t hash;
|
37
37
|
int klen;
|
38
38
|
} *MimeSlot;
|
39
39
|
|
40
|
-
typedef struct
|
40
|
+
typedef struct _cache {
|
41
41
|
Slot buckets[PAGE_BUCKET_SIZE];
|
42
42
|
MimeSlot muckets[MIME_BUCKET_SIZE];
|
43
43
|
char *root;
|
44
|
-
|
44
|
+
agooGroup groups;
|
45
45
|
} *Cache;
|
46
46
|
|
47
|
-
typedef struct
|
47
|
+
typedef struct _mime {
|
48
48
|
const char *suffix;
|
49
49
|
const char *type;
|
50
50
|
} *Mime;
|
51
51
|
|
52
52
|
// These are used for the initial load.
|
53
|
-
static struct
|
53
|
+
static struct _mime mime_map[] = {
|
54
54
|
{ "asc", "text/plain" },
|
55
55
|
{ "avi", "video/x-msvideo" },
|
56
56
|
{ "bin", "application/octet-stream" },
|
@@ -99,7 +99,7 @@ static struct _Mime mime_map[] = {
|
|
99
99
|
|
100
100
|
static const char page_fmt[] = "HTTP/1.1 200 OK\r\nContent-Type: %s\r\nContent-Length: %ld\r\n\r\n";
|
101
101
|
|
102
|
-
static struct
|
102
|
+
static struct _cache cache = {
|
103
103
|
.buckets = {0},
|
104
104
|
.muckets = {0},
|
105
105
|
.root = NULL,
|
@@ -162,13 +162,13 @@ mime_get(const char *key) {
|
|
162
162
|
return v;
|
163
163
|
}
|
164
164
|
|
165
|
-
|
165
|
+
agooPage
|
166
166
|
cache_get(const char *key, int klen) {
|
167
167
|
int len = klen;
|
168
168
|
int64_t h = calc_hash(key, &len);
|
169
169
|
Slot *bucket = get_bucketp(h);
|
170
170
|
Slot s;
|
171
|
-
|
171
|
+
agooPage v = NULL;
|
172
172
|
|
173
173
|
for (s = *bucket; NULL != s; s = s->next) {
|
174
174
|
if (h == (int64_t)s->hash && len == (int)s->klen &&
|
@@ -181,7 +181,7 @@ cache_get(const char *key, int klen) {
|
|
181
181
|
}
|
182
182
|
|
183
183
|
int
|
184
|
-
mime_set(
|
184
|
+
mime_set(agooErr err, const char *key, const char *value) {
|
185
185
|
int klen = (int)strlen(key);
|
186
186
|
int len = klen;
|
187
187
|
int64_t h = calc_hash(key, &len);
|
@@ -189,7 +189,7 @@ mime_set(Err err, const char *key, const char *value) {
|
|
189
189
|
MimeSlot s;
|
190
190
|
|
191
191
|
if (MAX_MIME_KEY_LEN < len) {
|
192
|
-
return
|
192
|
+
return agoo_err_set(err, AGOO_ERR_ARG, "%s is too long for a file extension. Maximum is %d", key, MAX_MIME_KEY_LEN);
|
193
193
|
}
|
194
194
|
for (s = *bucket; NULL != s; s = s->next) {
|
195
195
|
if (h == (int64_t)s->hash && len == s->klen &&
|
@@ -198,11 +198,11 @@ mime_set(Err err, const char *key, const char *value) {
|
|
198
198
|
DEBUG_FREE(mem_mime_slot, s->value);
|
199
199
|
free(s->value);
|
200
200
|
s->value = strdup(value);
|
201
|
-
return
|
201
|
+
return AGOO_ERR_OK;
|
202
202
|
}
|
203
203
|
}
|
204
|
-
if (NULL == (s = (MimeSlot)malloc(sizeof(struct
|
205
|
-
return
|
204
|
+
if (NULL == (s = (MimeSlot)malloc(sizeof(struct _mimeSlot)))) {
|
205
|
+
return agoo_err_set(err, AGOO_ERR_ARG, "out of memory adding %s", key);
|
206
206
|
}
|
207
207
|
DEBUG_ALLOC(mem_mime_slot, s);
|
208
208
|
s->hash = h;
|
@@ -216,16 +216,16 @@ mime_set(Err err, const char *key, const char *value) {
|
|
216
216
|
s->next = *bucket;
|
217
217
|
*bucket = s;
|
218
218
|
|
219
|
-
return
|
219
|
+
return AGOO_ERR_OK;
|
220
220
|
}
|
221
221
|
|
222
|
-
static
|
223
|
-
cache_set(const char *key, int klen,
|
222
|
+
static agooPage
|
223
|
+
cache_set(const char *key, int klen, agooPage value) {
|
224
224
|
int len = klen;
|
225
225
|
int64_t h = calc_hash(key, &len);
|
226
226
|
Slot *bucket = get_bucketp(h);
|
227
227
|
Slot s;
|
228
|
-
|
228
|
+
agooPage old = NULL;
|
229
229
|
|
230
230
|
if (MAX_KEY_LEN < len) {
|
231
231
|
return value;
|
@@ -241,7 +241,7 @@ cache_set(const char *key, int klen, Page value) {
|
|
241
241
|
return old;
|
242
242
|
}
|
243
243
|
}
|
244
|
-
if (NULL == (s = (Slot)malloc(sizeof(struct
|
244
|
+
if (NULL == (s = (Slot)malloc(sizeof(struct _slot)))) {
|
245
245
|
return value;
|
246
246
|
}
|
247
247
|
DEBUG_ALLOC(mem_page_slot, s)
|
@@ -261,11 +261,11 @@ cache_set(const char *key, int klen, Page value) {
|
|
261
261
|
}
|
262
262
|
|
263
263
|
void
|
264
|
-
|
264
|
+
agoo_pages_init() {
|
265
265
|
Mime m;
|
266
|
-
struct
|
266
|
+
struct _agooErr err = AGOO_ERR_INIT;
|
267
267
|
|
268
|
-
memset(&cache, 0, sizeof(struct
|
268
|
+
memset(&cache, 0, sizeof(struct _cache));
|
269
269
|
cache.root = strdup(".");
|
270
270
|
for (m = mime_map; NULL != m->suffix; m++) {
|
271
271
|
mime_set(&err, m->suffix, m->type);
|
@@ -273,7 +273,7 @@ pages_init() {
|
|
273
273
|
}
|
274
274
|
|
275
275
|
void
|
276
|
-
|
276
|
+
agoo_pages_set_root(const char *root) {
|
277
277
|
free(cache.root);
|
278
278
|
if (NULL == root) {
|
279
279
|
cache.root = NULL;
|
@@ -283,9 +283,9 @@ pages_set_root(const char *root) {
|
|
283
283
|
}
|
284
284
|
|
285
285
|
static void
|
286
|
-
|
286
|
+
agoo_page_destroy(agooPage p) {
|
287
287
|
if (NULL != p->resp) {
|
288
|
-
|
288
|
+
agoo_text_release(p->resp);
|
289
289
|
p->resp = NULL;
|
290
290
|
}
|
291
291
|
DEBUG_FREE(mem_page_path, p->path);
|
@@ -295,7 +295,7 @@ page_destroy(Page p) {
|
|
295
295
|
}
|
296
296
|
|
297
297
|
void
|
298
|
-
|
298
|
+
agoo_pages_cleanup() {
|
299
299
|
Slot *sp = cache.buckets;
|
300
300
|
Slot s;
|
301
301
|
Slot n;
|
@@ -308,7 +308,7 @@ pages_cleanup() {
|
|
308
308
|
for (s = *sp; NULL != s; s = n) {
|
309
309
|
n = s->next;
|
310
310
|
DEBUG_FREE(mem_page_slot, s);
|
311
|
-
|
311
|
+
agoo_page_destroy(s->value);
|
312
312
|
free(s);
|
313
313
|
}
|
314
314
|
*sp = NULL;
|
@@ -347,9 +347,9 @@ path_mime(const char *path) {
|
|
347
347
|
|
348
348
|
// The page resp points to the page resp msg to save memory and reduce
|
349
349
|
// allocations.
|
350
|
-
|
351
|
-
|
352
|
-
|
350
|
+
agooPage
|
351
|
+
agoo_page_create(const char *path) {
|
352
|
+
agooPage p = (agooPage)malloc(sizeof(struct _agooPage));
|
353
353
|
|
354
354
|
if (NULL != p) {
|
355
355
|
DEBUG_ALLOC(mem_page, p);
|
@@ -367,16 +367,16 @@ page_create(const char *path) {
|
|
367
367
|
return p;
|
368
368
|
}
|
369
369
|
|
370
|
-
|
371
|
-
|
372
|
-
|
370
|
+
agooPage
|
371
|
+
agoo_page_immutable(agooErr err, const char *path, const char *content, int clen) {
|
372
|
+
agooPage p = (agooPage)malloc(sizeof(struct _agooPage));
|
373
373
|
const char *mime = path_mime(path);
|
374
374
|
long msize;
|
375
375
|
int cnt;
|
376
376
|
int plen = 0;
|
377
377
|
|
378
378
|
if (NULL == p) {
|
379
|
-
|
379
|
+
agoo_err_set(err, AGOO_ERR_MEMORY, "Failed to allocate memory for page.");
|
380
380
|
return NULL;
|
381
381
|
}
|
382
382
|
DEBUG_ALLOC(mem_page, p);
|
@@ -400,10 +400,10 @@ page_immutable(Err err, const char *path, const char *content, int clen) {
|
|
400
400
|
// Format size plus space for the length, the mime type, and some
|
401
401
|
// padding. Then add the content length.
|
402
402
|
msize = sizeof(page_fmt) + 60 + clen;
|
403
|
-
if (NULL == (p->resp =
|
403
|
+
if (NULL == (p->resp = agoo_text_allocate((int)msize))) {
|
404
404
|
DEBUG_FREE(mem_page, p);
|
405
405
|
free(p);
|
406
|
-
|
406
|
+
agoo_err_set(err, AGOO_ERR_MEMORY, "Failed to allocate memory for page content.");
|
407
407
|
return NULL;
|
408
408
|
}
|
409
409
|
cnt = sprintf(p->resp->text, page_fmt, mime, (long)clen);
|
@@ -411,7 +411,7 @@ page_immutable(Err err, const char *path, const char *content, int clen) {
|
|
411
411
|
memcpy(p->resp->text + cnt, content, clen);
|
412
412
|
p->resp->text[msize] = '\0';
|
413
413
|
p->resp->len = msize;
|
414
|
-
|
414
|
+
agoo_text_ref(p->resp);
|
415
415
|
|
416
416
|
cache_set(path, plen, p);
|
417
417
|
|
@@ -419,18 +419,23 @@ page_immutable(Err err, const char *path, const char *content, int clen) {
|
|
419
419
|
}
|
420
420
|
|
421
421
|
static bool
|
422
|
-
|
422
|
+
close_return_false(FILE *f) {
|
423
|
+
fclose(f);
|
424
|
+
return false;
|
425
|
+
}
|
426
|
+
|
427
|
+
static bool
|
428
|
+
update_contents(agooPage p) {
|
423
429
|
const char *mime = path_mime(p->path);
|
424
430
|
int plen = (int)strlen(p->path);
|
425
|
-
FILE *f;
|
426
431
|
long size;
|
427
432
|
struct stat fattr;
|
428
433
|
long msize;
|
429
434
|
int cnt;
|
430
435
|
struct stat fs;
|
431
|
-
|
436
|
+
agooText t;
|
437
|
+
FILE *f = fopen(p->path, "rb");
|
432
438
|
|
433
|
-
f = fopen(p->path, "rb");
|
434
439
|
// On linux a directory is opened by fopen (sometimes? all the time?) so
|
435
440
|
// fstat is called to get the file mode and verify it is a regular file or
|
436
441
|
// a symlink.
|
@@ -467,28 +472,25 @@ update_contents(Page p) {
|
|
467
472
|
mime = "text/html";
|
468
473
|
}
|
469
474
|
if (0 != fseek(f, 0, SEEK_END)) {
|
470
|
-
|
471
|
-
return false;
|
475
|
+
return close_return_false(f);
|
472
476
|
}
|
473
477
|
if (0 > (size = ftell(f))) {
|
474
|
-
|
475
|
-
return false;
|
478
|
+
return close_return_false(f);
|
476
479
|
}
|
477
480
|
rewind(f);
|
478
481
|
|
479
482
|
// Format size plus space for the length, the mime type, and some
|
480
483
|
// padding. Then add the content length.
|
481
484
|
msize = sizeof(page_fmt) + 60 + size;
|
482
|
-
if (NULL == (t =
|
483
|
-
return
|
485
|
+
if (NULL == (t = agoo_text_allocate((int)msize))) {
|
486
|
+
return close_return_false(f);
|
484
487
|
}
|
485
488
|
cnt = sprintf(t->text, page_fmt, mime, size);
|
486
489
|
msize = cnt + size;
|
487
490
|
if (0 < size) {
|
488
491
|
if (size != (long)fread(t->text + cnt, 1, size, f)) {
|
489
|
-
|
490
|
-
|
491
|
-
return false;
|
492
|
+
agoo_text_release(t);
|
493
|
+
return close_return_false(f);
|
492
494
|
}
|
493
495
|
}
|
494
496
|
fclose(f);
|
@@ -500,18 +502,18 @@ update_contents(Page p) {
|
|
500
502
|
p->mtime = 0;
|
501
503
|
}
|
502
504
|
if (NULL != p->resp) {
|
503
|
-
|
505
|
+
agoo_text_release(p->resp);
|
504
506
|
p->resp = NULL;
|
505
507
|
}
|
506
508
|
p->resp = t;
|
507
|
-
|
509
|
+
agoo_text_ref(p->resp);
|
508
510
|
p->last_check = dtime();
|
509
511
|
|
510
512
|
return true;
|
511
513
|
}
|
512
514
|
|
513
515
|
static void
|
514
|
-
|
516
|
+
agoo_page_remove(agooPage p) {
|
515
517
|
int len = (int)strlen(p->path);
|
516
518
|
int64_t h = calc_hash(p->path, &len);
|
517
519
|
Slot *bucket = get_bucketp(h);
|
@@ -528,7 +530,7 @@ page_remove(Page p) {
|
|
528
530
|
prev->next = s->next;
|
529
531
|
}
|
530
532
|
DEBUG_FREE(mem_page_slot, s);
|
531
|
-
|
533
|
+
agoo_page_destroy(s->value);
|
532
534
|
free(s);
|
533
535
|
|
534
536
|
break;
|
@@ -537,8 +539,8 @@ page_remove(Page p) {
|
|
537
539
|
}
|
538
540
|
}
|
539
541
|
|
540
|
-
static
|
541
|
-
page_check(
|
542
|
+
static agooPage
|
543
|
+
page_check(agooErr err, agooPage page) {
|
542
544
|
if (!page->immutable) {
|
543
545
|
double now = dtime();
|
544
546
|
|
@@ -548,8 +550,8 @@ page_check(Err err, Page page) {
|
|
548
550
|
if (0 == stat(page->path, &fattr) && page->mtime != fattr.st_mtime) {
|
549
551
|
update_contents(page);
|
550
552
|
if (NULL == page->resp) {
|
551
|
-
|
552
|
-
|
553
|
+
agoo_page_remove(page);
|
554
|
+
agoo_err_set(err, AGOO_ERR_NOT_FOUND, "not found.");
|
553
555
|
return NULL;
|
554
556
|
}
|
555
557
|
}
|
@@ -559,16 +561,16 @@ page_check(Err err, Page page) {
|
|
559
561
|
return page;
|
560
562
|
}
|
561
563
|
|
562
|
-
|
563
|
-
|
564
|
-
|
564
|
+
agooPage
|
565
|
+
agoo_page_get(agooErr err, const char *path, int plen) {
|
566
|
+
agooPage page;
|
565
567
|
|
566
568
|
if (NULL != strstr(path, "../")) {
|
567
569
|
return NULL;
|
568
570
|
}
|
569
571
|
if (NULL == (page = cache_get(path, plen))) {
|
570
572
|
if (NULL != cache.root) {
|
571
|
-
|
573
|
+
agooPage old;
|
572
574
|
char full_path[2048];
|
573
575
|
char *s = stpcpy(full_path, cache.root);
|
574
576
|
|
@@ -576,22 +578,22 @@ page_get(Err err, const char *path, int plen) {
|
|
576
578
|
*s++ = '/';
|
577
579
|
}
|
578
580
|
if ((int)sizeof(full_path) <= plen + (s - full_path)) {
|
579
|
-
|
581
|
+
agoo_err_set(err, AGOO_ERR_MEMORY, "Failed to allocate memory for page path.");
|
580
582
|
return NULL;
|
581
583
|
}
|
582
584
|
strncpy(s, path, plen);
|
583
585
|
s[plen] = '\0';
|
584
|
-
if (NULL == (page =
|
585
|
-
|
586
|
+
if (NULL == (page = agoo_page_create(full_path))) {
|
587
|
+
agoo_err_set(err, AGOO_ERR_MEMORY, "Failed to allocate memory for agooPage.");
|
586
588
|
return NULL;
|
587
589
|
}
|
588
590
|
if (!update_contents(page) || NULL == page->resp) {
|
589
|
-
|
590
|
-
|
591
|
+
agoo_page_destroy(page);
|
592
|
+
agoo_err_set(err, AGOO_ERR_NOT_FOUND, "not found.");
|
591
593
|
return NULL;
|
592
594
|
}
|
593
595
|
if (NULL != (old = cache_set(path, plen, page))) {
|
594
|
-
|
596
|
+
agoo_page_destroy(old);
|
595
597
|
}
|
596
598
|
}
|
597
599
|
} else {
|
@@ -600,13 +602,13 @@ page_get(Err err, const char *path, int plen) {
|
|
600
602
|
return page;
|
601
603
|
}
|
602
604
|
|
603
|
-
|
604
|
-
group_get(
|
605
|
-
|
606
|
-
|
605
|
+
agooPage
|
606
|
+
group_get(agooErr err, const char *path, int plen) {
|
607
|
+
agooPage page = NULL;
|
608
|
+
agooGroup g = NULL;
|
607
609
|
char full_path[2048];
|
608
610
|
char *s = NULL;
|
609
|
-
|
611
|
+
agooDir d;
|
610
612
|
|
611
613
|
if (NULL != strstr(path, "../")) {
|
612
614
|
return NULL;
|
@@ -650,19 +652,19 @@ group_get(Err err, const char *path, int plen) {
|
|
650
652
|
plen = (int)(s - full_path);
|
651
653
|
path = full_path;
|
652
654
|
if (NULL == (page = cache_get(path, plen))) {
|
653
|
-
|
655
|
+
agooPage old;
|
654
656
|
|
655
|
-
if (NULL == (page =
|
656
|
-
|
657
|
+
if (NULL == (page = agoo_page_create(path))) {
|
658
|
+
agoo_err_set(err, AGOO_ERR_MEMORY, "Failed to allocate memory for agooPage.");
|
657
659
|
return NULL;
|
658
660
|
}
|
659
661
|
if (!update_contents(page) || NULL == page->resp) {
|
660
|
-
|
661
|
-
|
662
|
+
agoo_page_destroy(page);
|
663
|
+
agoo_err_set(err, AGOO_ERR_NOT_FOUND, "not found.");
|
662
664
|
return NULL;
|
663
665
|
}
|
664
666
|
if (NULL != (old = cache_set(path, plen, page))) {
|
665
|
-
|
667
|
+
agoo_page_destroy(old);
|
666
668
|
}
|
667
669
|
}
|
668
670
|
return page;
|
@@ -670,9 +672,9 @@ group_get(Err err, const char *path, int plen) {
|
|
670
672
|
return page_check(err, page);
|
671
673
|
}
|
672
674
|
|
673
|
-
|
675
|
+
agooGroup
|
674
676
|
group_create(const char *path) {
|
675
|
-
|
677
|
+
agooGroup g = (agooGroup)malloc(sizeof(struct _agooGroup));
|
676
678
|
|
677
679
|
if (NULL != g) {
|
678
680
|
DEBUG_ALLOC(mem_group, g);
|
@@ -687,8 +689,8 @@ group_create(const char *path) {
|
|
687
689
|
}
|
688
690
|
|
689
691
|
void
|
690
|
-
group_add(
|
691
|
-
|
692
|
+
group_add(agooGroup g, const char *dir) {
|
693
|
+
agooDir d = (agooDir)malloc(sizeof(struct _agooDir));
|
692
694
|
|
693
695
|
if (NULL != d) {
|
694
696
|
DEBUG_ALLOC(mem_dir, d);
|