-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
51 lines (47 loc) · 1.36 KB
/
main.c
File metadata and controls
51 lines (47 loc) · 1.36 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libpiper/server.h>
// Internal Error
static void internal_error(int sock) {
piper_response response;
response.content_type = SERVER_INTERNAL_ERROR;
response.content_length = 0;
piper_server_respond(sock, &response);
}
// Callback
static int callback(piper_request *request, int sock, __attribute__((unused)) void *user_data) {
// Log
printf("New Connection: %s\n", request->path);
// Choose Behavior
if (strcmp("/redirect", request->path) == 0) {
// Redirect At "/redirect"
if (piper_server_respond_str(sock, REDIRECT, "/hello") != 0) {
internal_error(sock);
}
} else if (strcmp("/shutdown", request->path) == 0) {
// Shutdown Server
if (piper_server_respond_str(sock, UTF8_TEXT, "Goodbye.") != 0) {
internal_error(sock);
}
return 1;
} else {
// Everything Else
if (piper_server_respond_str(sock, UTF8_TEXT, "Hello From \"%s\"!", request->path) != 0) {
internal_error(sock);
}
}
// Return
return 0;
}
// Main
int main() {
printf("Starting...\n");
// Run
if (piper_server_run(60, 10, callback, NULL) != 0) {
fprintf(stderr, "Failed To Start Server\n");
return EXIT_FAILURE;
} else {
return EXIT_SUCCESS;
}
}