/
status.c
86 lines (70 loc) · 1.91 KB
/
status.c
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
/*
Copyright (C) 2009 Conrad Parker
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <string.h>
#include "http-reqline.h"
#include "http-status.h"
#include "params.h"
#include "resource.h"
#include "sighttpd.h"
#define STATUS_HEAD \
"<html>\n" \
"<head>\n" \
"<title>Sighttpd/%s - Status</title>\n" \
"</head>\n" \
"<body>\n" \
"<h1>Sighttpd/%s Status</h1>\n"
#define STATUS_FOOT \
"</body>\n" \
"</head>\n"
static int
status_check (http_request * request, void * data)
{
return !strncmp (request->path, "/status", 7);
}
static params_t *
status_append_headers (params_t * response_headers)
{
char length[16];
response_headers = params_append (response_headers, "Content-Type", "text/html");
return response_headers;
}
static void
status_head (http_request * request, params_t * request_headers, const char ** status_line,
params_t ** response_headers, void * data)
{
*status_line = http_status_line (HTTP_STATUS_OK);
*response_headers = status_append_headers (*response_headers);
}
static int
status_body (int fd, http_request * request, params_t * request_headers, void * data)
{
char buf[4096];
int n, ntotal=0;
struct sighttpd * sighttpd = (struct sighttpd *)data;
n = snprintf (buf, 4096, STATUS_HEAD, VERSION, VERSION);
if (n < 0) return -1;
if (n > 4096) n = 4096;
ntotal += n;
write (fd, buf, n);
n = snprintf (buf, 4096, "<p>Active resources: %d</p>\n", list_length (sighttpd->resources));
if (n < 0) return -1;
if (n > 4096) n = 4096;
ntotal += n;
write (fd, buf, n);
n = snprintf (buf, 4096, STATUS_FOOT, VERSION, VERSION);
if (n < 0) return -1;
if (n > 4096) n = 4096;
ntotal += n;
write (fd, buf, n);
return ntotal;
}
struct resource *
status_resource (struct sighttpd * sighttpd)
{
return resource_new (status_check, status_head, status_body, NULL /* del */, sighttpd);
}