-
Notifications
You must be signed in to change notification settings - Fork 3
/
gohorse.c
150 lines (127 loc) · 5.76 KB
/
gohorse.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
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
// -*- coding: utf-8 -*-
// <gohorse - cowboy http framework for c lovers>
// Copyright (C) <2010> Gabriel Falcão <gabriel@nacaolivre.org>
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#include <gohorse/gohorse.h>
GHashTable *possible_responses = NULL;
void gh_exit_program (int sig) {
printf("\033[D\033[D\033[1;31mserver was shutdown\033[0m\n");
exit(sig);
}
int _connection_values_to_hash_table (gpointer cls,
enum MHD_ValueKind kind,
const gchar *key,
const gchar *value)
{
g_hash_table_replace((GHashTable *)cls, (gchar *)key, (gchar *)value);
return MHD_YES;
}
gboolean _gh_match_response (gpointer key, gpointer value, gpointer user_data)
{
GHRequest *request = user_data;
gchar *regex_string = key;
gboolean matches = g_regex_match_simple(regex_string, request->url, G_REGEX_EXTENDED, G_REGEX_MATCH_ANCHORED);
GHResponse *response = NULL;
GHResponseCallback callback = value;
if (matches == TRUE) {
response = callback(request);
request->queue_code = MHD_queue_response(request->connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
time_t curtime;
struct tm *loctime;
curtime = time(NULL);
loctime = localtime(&curtime);
printf("\033[1;37m%s \033[1;33m%s\033[0m - \033[1;32m200 OK\033[0m - \033[1;30m%s\033[0m", request->method, request->url, asctime(loctime));
return TRUE;
}
return FALSE;
}
int gh_response (void *cls,
GHConnection *connection,
const char *url,
const char *method,
const char *version,
const char *upload_data,
size_t *upload_data_size,
void **con_cls)
{
GHResponse *response = NULL;
GHRequest *request = g_new0(GHRequest, 1);
request->connection = connection;
request->url = (gchar *)url;
request->method = (gchar *)method;
request->http_version = (gchar *)version;
request->upload_data = (gchar *)upload_data;
request->upload_data_size = upload_data_size;
request->parameters = g_hash_table_new(g_str_hash, g_str_equal);
request->cookies = g_hash_table_new(g_str_hash, g_str_equal);
request->post_data = g_hash_table_new(g_str_hash, g_str_equal);
request->queue_code = 0;
MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND, &_connection_values_to_hash_table, request->parameters);
MHD_get_connection_values(connection, MHD_POSTDATA_KIND, &_connection_values_to_hash_table, request->post_data);
MHD_get_connection_values(connection, MHD_COOKIE_KIND, &_connection_values_to_hash_table, request->cookies);
if ((possible_responses !=NULL) && (g_hash_table_find(possible_responses, _gh_match_response, (gpointer)request) != NULL)) {
g_hash_table_unref(request->parameters);
g_hash_table_unref(request->post_data);
g_hash_table_unref(request->cookies);
if (request->queue_code != 0) {
return request->queue_code;
}
}
int queue_code;
const gchar* response404 = "<html><head></head><body><h1>404 - not found</h1></body></html>";
response = MHD_create_response_from_data(strlen(response404), (void*)response404, MHD_NO, MHD_NO);
queue_code = MHD_queue_response(request->connection, 404, response);
time_t curtime;
struct tm *loctime;
curtime = time (NULL);
loctime = localtime (&curtime);
printf("\033[1;37m%s \033[1;33m%s\033[0m - \033[1;31m404 NOT FOUND\033[0m - \033[1;30m%s\033[0m", request->method, request->url, asctime(loctime));
return queue_code;
}
void _gh_register_response(const gchar* regex, GHResponseCallback callback, gpointer data){
if (possible_responses == NULL) {
possible_responses = g_hash_table_new(g_str_hash, g_str_equal);
}
g_hash_table_replace(possible_responses, (gchar *)regex, callback);
}
GOHorseDaemon *gh_run_server(int port) {
GOHorseDaemon *d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, port, NULL, NULL, &gh_response, NULL, MHD_OPTION_END);
if (d is NULL) {
printf("Could not start GO Horse server on port %d\n", port);
exit(1);
}
printf("GO Horse is running on localhost:%d\n", port);
signal(SIGABRT, gh_exit_program);
signal(SIGTERM, gh_exit_program);
signal(SIGINT, gh_exit_program);
while (true) {getchar();}
}
void gh_stop_server (GOHorseDaemon *daemon) {
MHD_stop_daemon(daemon);
}
GHResponse* render(const gchar *string, ...){
va_list elements;
gchar *rendered = NULL;
va_start(elements, string);
gint length = g_vasprintf(&rendered, string, elements);
va_end(elements);
return MHD_create_response_from_data(length, (void*)rendered, MHD_YES, MHD_YES);
}