Skip to content

Commit d2cf8d2

Browse files
author
Tom Hinchliff
committed
Actions now return true if they have been 'fullfilled'.
1 parent 7a5f8f6 commit d2cf8d2

File tree

4 files changed

+45
-63
lines changed

4 files changed

+45
-63
lines changed

actions.c

Lines changed: 31 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <string.h>
44
#include <unistd.h>
55
#include <signal.h>
6+
#include <stdbool.h>
67

78
#include <sys/socket.h>
89
#include <sys/stat.h>
@@ -43,16 +44,17 @@ static const inline int file_exists(const char *path)
4344
return (stat(path, &buffer) == 0);
4445
}
4546

46-
void generic_action(int client_socket, HTTP_REQUEST req)
47+
bool generic_action(int client_socket, HTTP_REQUEST req)
4748
{
4849
// Action data.
4950
send(client_socket, HTTP_OK_RESPONSE,
5051
strlen(HTTP_OK_RESPONSE), 0);
52+
return true;
5153
}
5254

5355
// A generic 204, let the client know that we accepted the message, but there
5456
// is no content.
55-
void ok_action(int client_socket, HTTP_REQUEST req)
57+
bool ok_action(int client_socket, HTTP_REQUEST req)
5658
{
5759
if (req.body)
5860
{
@@ -62,19 +64,21 @@ void ok_action(int client_socket, HTTP_REQUEST req)
6264
// Action data.
6365
send(client_socket, HTTP_OK_TEXT_RESPONSE,
6466
strlen(HTTP_OK_TEXT_RESPONSE), 0);
67+
return true;
6568
}
6669

6770
// Error processing or no match, simply a 404.
68-
void not_found_action(int client_socket, HTTP_REQUEST req)
71+
bool not_found_action(int client_socket, HTTP_REQUEST req)
6972
{
7073
DEBUG_LOG("404 action!!\n");
7174

7275
// Action data.
7376
send(client_socket, HTTP_404_RESPONSE,
74-
strlen(HTTP_404_RESPONSE), 0);
77+
strlen(HTTP_404_RESPONSE), 0);
78+
return true;
7579
}
7680

77-
void sample_github_action(int client_socket, HTTP_REQUEST req)
81+
bool sample_github_action(int client_socket, HTTP_REQUEST req)
7882
{
7983
static const char *repo_id = "276006277";
8084
static const char *script_path = "../scripts/update_http_server.sh";
@@ -84,11 +88,7 @@ void sample_github_action(int client_socket, HTTP_REQUEST req)
8488

8589
// Parse the body, if it does not exist (not valid POST) return.
8690
if (!req.body)
87-
{
88-
send(client_socket, HTTP_404_RESPONSE,
89-
strlen(HTTP_404_RESPONSE), 0);
90-
return;
91-
}
91+
return false;
9292

9393
char *uncoded_body = (char *)malloc(sizeof(char *) * strlen(req.body));
9494
convert_from_utf8(req.body, uncoded_body);
@@ -97,27 +97,21 @@ void sample_github_action(int client_socket, HTTP_REQUEST req)
9797
if (id == NULL)
9898
{
9999
free(uncoded_body);
100-
send(client_socket, HTTP_404_RESPONSE,
101-
strlen(HTTP_404_RESPONSE), 0);
102-
return;
100+
return false;
103101
}
104102
id += strlen(needle);
105103
char *end = strchr(id, ',');
106104
if (end == NULL)
107105
{
108106
free(uncoded_body);
109-
send(client_socket, HTTP_404_RESPONSE,
110-
strlen(HTTP_404_RESPONSE), 0);
111-
return;
107+
return false;
112108
}
113109
end[0] = '\0';
114110

115111
if (strcmp(id, repo_id) != 0)
116112
{
117113
DEBUG_LOG("GitHub id %s does not match %s\n", id, repo_id);
118-
send(client_socket, HTTP_404_RESPONSE,
119-
strlen(HTTP_404_RESPONSE), 0);
120-
return;
114+
return false;
121115
}
122116
free(uncoded_body);
123117

@@ -127,7 +121,7 @@ void sample_github_action(int client_socket, HTTP_REQUEST req)
127121
ERROR_LOG("File %s not found\n", script_path);
128122
send(client_socket, HTTP_500_RESPONSE,
129123
strlen(HTTP_500_RESPONSE), 0);
130-
return;
124+
return true;
131125
}
132126

133127
int pid = fork();
@@ -140,9 +134,10 @@ void sample_github_action(int client_socket, HTTP_REQUEST req)
140134
DEBUG_LOG("Success! Letting GitHub know!\n");
141135
send(client_socket, HTTP_OK_RESPONSE,
142136
strlen(HTTP_OK_RESPONSE), 0);
137+
return true;
143138
}
144139

145-
void update_server_action(int client_socket, HTTP_REQUEST req)
140+
bool update_server_action(int client_socket, HTTP_REQUEST req)
146141
{
147142
static const char *repo_id = "276006277";
148143
static const char *script_path = "../scripts/update_http_server.sh";
@@ -151,11 +146,7 @@ void update_server_action(int client_socket, HTTP_REQUEST req)
151146

152147
// Parse the body, if it does not exist (not valid POST) return.
153148
if (!req.body)
154-
{
155-
send(client_socket, HTTP_404_RESPONSE,
156-
strlen(HTTP_404_RESPONSE), 0);
157-
return;
158-
}
149+
return false;
159150

160151
char *uncoded_body = (char *)malloc(sizeof(char *) * strlen(req.body));
161152
convert_from_utf8(req.body, uncoded_body);
@@ -164,27 +155,22 @@ void update_server_action(int client_socket, HTTP_REQUEST req)
164155
if (id == NULL)
165156
{
166157
free(uncoded_body);
167-
send(client_socket, HTTP_404_RESPONSE,
168-
strlen(HTTP_404_RESPONSE), 0);
169-
return;
158+
return false;
170159
}
171160
id += strlen(needle);
172161
char *end = strchr(id, ',');
173162
if (end == NULL)
174163
{
175164
free(uncoded_body);
176-
send(client_socket, HTTP_404_RESPONSE,
177-
strlen(HTTP_404_RESPONSE), 0);
178-
return;
165+
return false;
179166
}
180167
end[0] = '\0';
181168

182169
if (strcmp(id, repo_id) != 0)
183170
{
184171
DEBUG_LOG("GitHub id %s does not match %s\n", id, repo_id);
185-
send(client_socket, HTTP_404_RESPONSE,
186-
strlen(HTTP_404_RESPONSE), 0);
187-
return;
172+
free(uncoded_body);
173+
return false;
188174
}
189175
free(uncoded_body);
190176

@@ -194,7 +180,7 @@ void update_server_action(int client_socket, HTTP_REQUEST req)
194180
ERROR_LOG("File %s not found\n", script_path);
195181
send(client_socket, HTTP_500_RESPONSE,
196182
strlen(HTTP_500_RESPONSE), 0);
197-
return;
183+
return true;
198184
}
199185

200186
DEBUG_LOG("Success! Letting GitHub know!\n");
@@ -203,9 +189,10 @@ void update_server_action(int client_socket, HTTP_REQUEST req)
203189

204190
// Not great since we don't clean up but the alternative is a callback.
205191
execl("/bin/sh", "sh", script_path, NULL);
192+
return true;
206193
}
207194

208-
void update_website_action(int client_socket, HTTP_REQUEST req)
195+
bool update_website_action(int client_socket, HTTP_REQUEST req)
209196
{
210197
static const char *web_repo_id = "275560807";
211198
static const char *blog_repo_id = "275565965";
@@ -215,11 +202,7 @@ void update_website_action(int client_socket, HTTP_REQUEST req)
215202

216203
// Parse the body, if it does not exist (not valid POST) return.
217204
if (!req.body)
218-
{
219-
send(client_socket, HTTP_404_RESPONSE,
220-
strlen(HTTP_404_RESPONSE), 0);
221-
return;
222-
}
205+
return false;
223206

224207
char *uncoded_body = (char *)malloc(sizeof(char *) * strlen(req.body));
225208
convert_from_utf8(req.body, uncoded_body);
@@ -228,27 +211,22 @@ void update_website_action(int client_socket, HTTP_REQUEST req)
228211
if (id == NULL)
229212
{
230213
free(uncoded_body);
231-
send(client_socket, HTTP_404_RESPONSE,
232-
strlen(HTTP_404_RESPONSE), 0);
233-
return;
214+
return false;
234215
}
235216
id += strlen(needle);
236217
char *end = strchr(id, ',');
237218
if (end == NULL)
238219
{
239220
free(uncoded_body);
240-
send(client_socket, HTTP_404_RESPONSE,
241-
strlen(HTTP_404_RESPONSE), 0);
242-
return;
221+
return false;
243222
}
244223
end[0] = '\0';
245224

246225
if (strcmp(id, web_repo_id) != 0 && strcmp(id, blog_repo_id) != 0)
247226
{
227+
free(uncoded_body);
248228
DEBUG_LOG("GitHub id %s does not match provided repository ids\n", id);
249-
send(client_socket, HTTP_404_RESPONSE,
250-
strlen(HTTP_404_RESPONSE), 0);
251-
return;
229+
return false;
252230
}
253231
free(uncoded_body);
254232

@@ -258,7 +236,7 @@ void update_website_action(int client_socket, HTTP_REQUEST req)
258236
ERROR_LOG("File %s not found\n", script_path);
259237
send(client_socket, HTTP_500_RESPONSE,
260238
strlen(HTTP_500_RESPONSE), 0);
261-
return;
239+
return true;
262240
}
263241

264242
int pid = fork();
@@ -271,6 +249,5 @@ void update_website_action(int client_socket, HTTP_REQUEST req)
271249
DEBUG_LOG("Success! Letting GitHub know!\n");
272250
send(client_socket, HTTP_OK_RESPONSE,
273251
strlen(HTTP_OK_RESPONSE), 0);
252+
return true;
274253
}
275-
276-

actions.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
#ifndef ACTIONS_H
22
#define ACTIONS_H
33

4+
#include <stdbool.h>
5+
46
#include "http.h"
57

6-
void generic_action(int client_socket, HTTP_REQUEST req);
8+
bool generic_action(int client_socket, HTTP_REQUEST req);
79

8-
void ok_action(int client_socket, HTTP_REQUEST req);
10+
bool ok_action(int client_socket, HTTP_REQUEST req);
911

10-
void not_found_action(int client_socket, HTTP_REQUEST req);
12+
bool not_found_action(int client_socket, HTTP_REQUEST req);
1113

12-
void sample_github_action(int client_socket, HTTP_REQUEST req);
14+
bool sample_github_action(int client_socket, HTTP_REQUEST req);
1315

14-
void update_server_action(int client_socket, HTTP_REQUEST req);
16+
bool update_server_action(int client_socket, HTTP_REQUEST req);
1517

1618
#endif

filter.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void filter_request(int client_socket, HTTP_REQUEST req, HTTP_RESPONSE *res)
4242
{
4343
// Match the value.
4444
if (strcmp(filters[i].headers[j].value,
45-
req.headers[k].value) == 0)
45+
req.headers[k].value) == 0)
4646
match--;
4747
break;
4848
}
@@ -51,8 +51,9 @@ void filter_request(int client_socket, HTTP_REQUEST req, HTTP_RESPONSE *res)
5151
if (match == 0)
5252
{
5353
// Execute the action and exit the function.
54-
filters[i].action(client_socket, req);
55-
return;
54+
if (filters[i].action(client_socket, req)) {
55+
return;
56+
}
5657
}
5758
}
5859

filter.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef FILTER_H
22
#define FILTER_H
33

4+
#include <stdbool.h>
5+
46
#include "config.h"
57
#include "actions.h"
68
#include "http.h"
@@ -28,7 +30,7 @@ typedef struct REQUEST_FILTER
2830
const char *host;
2931
HTTP_HEADER headers[MAX_HEADERS];
3032
const int num_headers;
31-
void (*const action)(int, HTTP_REQUEST);
33+
bool (*const action)(int, HTTP_REQUEST);
3234
} REQUEST_FILTER;
3335

3436
// Filters will be defined here.

0 commit comments

Comments
 (0)