-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong.c
140 lines (115 loc) · 3.65 KB
/
pong.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
/* Feel free to use this example code in any way
you see fit (Public Domain) */
#include <sys/types.h>
#ifndef _WIN32
#include <sys/select.h>
#include <sys/socket.h>
#else
#include <winsock2.h>
#endif
#include <microhttpd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <json-c/json.h>
#if defined(_MSC_VER) && _MSC_VER + 0 <= 1800
/* Substitution is OK while return value is not used */
#define snprintf _snprintf
#endif
#define PORT 8888
struct postStatus
{
int len;
bool status;
char *buffer;
};
static int
send_page(struct MHD_Connection *connection, const char *page, int status_code, enum MHD_ResponseMemoryMode mode, char *mimetype)
{
int ret;
struct MHD_Response *response;
response =
MHD_create_response_from_buffer(strlen(page), (void *)page,
mode);
if (!response)
return MHD_NO;
MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
ret = MHD_queue_response(connection, status_code, response);
MHD_destroy_response(response);
return ret;
}
static int
answer_to_connection(void *cls, struct MHD_Connection *connection,
const char *url, const char *method,
const char *version, const char *upload_data,
size_t *upload_data_size, void **con_cls)
{
struct postStatus *post = (struct postStatus *)*con_cls;
if (strcmp(method, "POST") == 0)
{
const char *param = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_LENGTH);
int length_of_content = atoi(param);
const char *req_content_type = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "Content-Type");
if (strcmp(url, "/hello") != 0)
return send_page(connection, "Not found: using url [ip]:8888/hello", MHD_HTTP_NOT_FOUND, MHD_RESPMEM_PERSISTENT, "application/json");
if (post == NULL)
{
post = (struct postStatus *)calloc(1, sizeof(struct postStatus));
post->status = false;
post->len = 0;
post->buffer = (char *)calloc(length_of_content + 1, sizeof(char));
*con_cls = post;
}
if (!post->status)
{
post->status = true;
return MHD_YES;
}
if (*upload_data_size != 0)
{
memcpy(post->buffer + post->len, upload_data, *upload_data_size);
post->len = post->len + *upload_data_size;
post->buffer[post->len] = '\0';
*upload_data_size = 0;
return MHD_YES;
}
else
{
if (post->buffer)
{
printf("Request \n");
if (strcmp(req_content_type, "application/json") == 0)
{
char *response_page = strdup(post->buffer);
free(post->buffer);
free(post);
static int count = 0;
printf("Response %d\n", count++);
int ret = send_page(connection, response_page, MHD_HTTP_OK, MHD_RESPMEM_MUST_FREE, "application/json");
return ret;
}
else
{
free(post->buffer);
free(post);
return send_page(connection, "Bad request", MHD_HTTP_BAD_REQUEST, MHD_RESPMEM_PERSISTENT, "application/json");
}
}
}
}
return MHD_NO;
}
int main()
{
struct MHD_Daemon *daemon;
daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL,
&answer_to_connection, NULL,
MHD_OPTION_END);
if (NULL == daemon)
return 1;
printf("\n\n\nhttpd up\n press any key to exit\n");
(void)getchar();
MHD_stop_daemon(daemon);
return 0;
}