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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
#include <stdbool.h>
#include <stdarg.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <microhttpd.h>
#define CONTAINER_IMPLEMENTATION
#define JACSON_IMPLEMENTATION
#define JACSON_EXPORT_RSNPRINTF
#include <jacson.h>
#define COMMON_IMPLEMENTATION
#include <common.h>
const char *const JSON_ERROR = "{\"error\":\"%s\"}";
const char *const HTML_ERROR = "<!DOCTYPE html><html><body>Error: %s</body></html>";
const char *const JSON_SERVER_ERROR = "{\"error\":\"%s\",\"what\":\"%s\"}";
const char *const HTML_SERVER_ERROR = "<!DOCTYPE html><html><body>Error: %s<br>Server error message: <p style=\"font-style: monospace\">%s</p></body></html>";
const char *const INSUFFICIENT_ARGUMENTS = "insufficient arguments";
const char *const INVALID_ARGUMENTS = "invalid arguments";
const char *const OCCUPIED = "occupied";
const char *const SERVER_ERRORMSG = "server error";
const char *const UNKNOWN = "unknown";
const char *const HTML_PROLOGUE = "<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head><body>";
const char *const HTML_EPILOGUE = "</html></body>";
struct global_args {
enum {
GA_FMT_JSON = 0,
GA_FMT_HTML = 1
} format;
};
struct fmt_const {
const char *error, *server_error, *content_type;
};
static struct fmt_const fmts[] = {
[GA_FMT_JSON] = {JSON_ERROR, JSON_SERVER_ERROR, "application/json"},
[GA_FMT_HTML] = {HTML_ERROR, HTML_SERVER_ERROR, "text/html"}
};
// string builder
struct sb {
char* str;
size_t sz;
size_t ln;
};
size_t sbprintf(struct sb* s, const char* fmt, ...) {
va_list l;
va_start(l, fmt);
return s->ln += __jacson_vrsnprintf(&s->str, &s->sz, s->ln, fmt, l);
}
enum MHD_Result arg_builder (void *cls, enum MHD_ValueKind kind, const char *key, const char *value) {
struct sb *html = cls;
html->ln += __jacson_rsnprintf(&html->str, &html->sz, html->ln, "<li><p style=\"font-style:monospace;\">%s: %s</p></li>", key, value);
return MHD_YES;
}
struct global_args parse_global_args(struct MHD_Connection *connection) {
const char* key, *val;
struct global_args ret = { .format = GA_FMT_JSON };
key = "format";
if ( (val = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, key))
&& !strcmp(val, "html")) ret.format = GA_FMT_HTML;
return ret;
}
char *db_add_url(const char *url, const char *path) {
return "test";
}
bool db_del_url(const char *path) {
return false;
}
// dong: output parameter
char *db_get_info(const char *path, long *dong) {
*dong = 0;
return NULL;
}
char *db_get_url(const char *path) {
return NULL;
}
// alphabet: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
// returns 0 if no conversion is available
static inline char itoc(register unsigned long l) {
#ifdef __GNUC__
char add[256] = {
[0 ... 9] = '0',
[10 ... 35] = 'a' - 10,
[36 ... 61] = 'A' - 10 - 26,
};
return (l < 62) * (l + add[l]);
#else
if (l < 10) return '0' + l;
if (l < 10 + 26) return 'a' + l - 10;
if (l < 10 + 2 * 26) return 'A' + l - 10 - 26;
return '\0';
#endif
}
// returns negative number if conversion not available
static inline unsigned long ctoi(register char c) {
#ifdef __GNUC__
char sub[127] = {
[0 ... '0'-1] = 128,
['0' ... '9'] = '0',
['A' ... 'Z'] = 'A' - 26 - 10,
['a' ... 'z'] = 'a' - 10,
};
return c - sub[c];
#else
TODO;
#endif
}
// buf must be at least 12 characters (11 for strnig and 1 for termination byte)
// returns a pointer from which the string starts
static char *itou(register unsigned long i, register char *buf) {
register ssize_t ind = 11;
buf[ind] = '\0';
buf[ind - 1] = '0';
while (ind >= 0 && i) {
buf[--ind] = itoc(i % 62);
i /= 62;
}
return buf + ind;
}
static unsigned long utoi(const char *s) {
register unsigned long result = 0;
while (*s) {
result *= 62;
result += ctoi(*s);
++s;
}
return result;
}
// index, path: /
struct MHD_Response *ENDP_(struct MHD_Connection* connection, int *status) {
return get_from_file("www/index.html");
}
// path: /getargs
struct MHD_Response *ENDP_getargs(struct MHD_Connection *connection, int *status) {
struct sb html = {calloc(64, 1), 63, 0};
html.ln += __jacson_rsnprintf(&html.str, &html.sz, html.ln,
"<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head><body><h3>List of arguments</h3><ol>");
MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND, &arg_builder, &html);
html.ln += __jacson_rsnprintf(&html.str, &html.sz, html.ln, "</ol></body></html>");
const char *val = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "test");
if (val) fprintf(stderr, "test = %s\n", val);
fprintf(stderr, "test pointer = %p", val);
return MHD_create_response_from_buffer(html.ln, html.str, MHD_RESPMEM_MUST_FREE);
}
// API, path: /api/linkadd
struct MHD_Response *ENDP_api_linkadd(struct MHD_Connection *connection, int *status) {
const char *url = NULL, *path = NULL;
char *newurl;
struct sb resp = {malloc(64), 63, 0};
struct global_args glob = parse_global_args(connection);
struct MHD_Response *response;
if ((url = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "url")) == NULL || *url == '\0') {
*status = MHD_HTTP_BAD_REQUEST;
sbprintf(&resp, fmts[glob.format].error, INSUFFICIENT_ARGUMENTS);
goto exit;
}
path = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "try");
if ((newurl = db_add_url(url, path)) == NULL) {
*status = MHD_HTTP_INTERNAL_SERVER_ERROR;
sbprintf(&resp, fmts[glob.format].server_error, SERVER_ERRORMSG, "database write failed");
goto exit;
}
if (glob.format == GA_FMT_JSON) sbprintf(&resp, "{\"url\":\"%s\"}", newurl);
else sbprintf(&resp, "%sYour URL: <a href\"%s\">/%s</a>%s", HTML_PROLOGUE, newurl, newurl, HTML_EPILOGUE);
exit:
response = MHD_create_response_from_buffer(resp.ln, resp.str, MHD_RESPMEM_MUST_FREE);
MHD_add_response_header(response, "Content-Type", fmts[glob.format].content_type);
return response;
}
// API, path: /api/linkdel
struct MHD_Response *ENDP_api_linkdel(struct MHD_Connection *connection, int *status) {
}
// API, path: /api/linkget
struct MHD_Response *ENDP_api_linkget(struct MHD_Connection *connection, int *status) { }
|