Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
server.c
Go to the documentation of this file.
1/**
2 * @file server.c
3 * @brief Implements a simple HTTP server using Civetweb to serve GitHub user
4 * information.
5 */
6
7#include "server.h"
9#include "user.h"
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <unistd.h>
15
16/**
17 * @brief HTTP request handler for user lookup.
18 *
19 * Parses the request URI to extract the username, fetches the corresponding
20 * GitHub user, and responds with a JSON object.
21 *
22 * @param conn The connection to the client.
23 * @param cbdata Callback data (unused).
24 * @return HTTP status code (e.g., 200 for success, 400 for bad request).
25 */
26static int user_handler(struct mg_connection *conn, void *cbdata) {
27 (void)cbdata; // Mark parameter as deliberately unused
28 const struct mg_request_info *req_info = mg_get_request_info(conn);
29 const char *uri = req_info->local_uri;
30
31 // Validate that the URI is valid and null-terminated
32 size_t uri_len = strnlen(uri, 1024); // Limit max length to avoid over-read
33 if (uri_len < 2) {
34 mg_printf(conn, "HTTP/1.1 400 Bad Request\r\nContent-Type: "
35 "text/plain\r\n\r\nMissing username\n");
36 return 400;
37 }
38
39 const char *username = uri + 1; // skip the initial '/'
40 User user = fetch_github_user(username);
41
42 mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n");
43 mg_printf(conn,
44 "{\n \"login\": \"%s\",\n \"name\": \"%s\",\n \"company\": "
45 "\"%s\",\n \"location\": \"%s\"\n}\n",
46 user.login, user.name, user.company, user.location);
47
48 return 200;
49}
50
51/**
52 * @brief Starts an HTTP server on the specified port.
53 *
54 * Configures Civetweb with basic options and handles incoming user requests.
55 * The server runs indefinitely until manually interrupted.
56 *
57 * @param port The TCP port to listen on.
58 */
59void start_http_server(int port) {
60 char *port_str = NULL;
61 if (asprintf(&port_str, "%d", port) == -1) {
62 (void)fprintf(stderr, "Failed to allocate memory for port\n");
63 return;
64 }
65
66 const char *options[] = {"listening_ports",
67 port_str,
68 "enable_keep_alive",
69 "yes",
70 "access_log_file",
71 "access.log",
72 NULL};
73
74 struct mg_callbacks callbacks = {0};
75 struct mg_context *ctx = mg_start(&callbacks, NULL, options);
76
77 if (!ctx) {
78 (void)fprintf(stderr, "Failed to start Civetweb server\n");
79 return;
80 }
81
83
84 printf("Server started on port %s. Visit http://localhost:%s/USERNAME\n",
85 port_str, port_str);
86 puts("Press Ctrl+C to quit.");
87 free(port_str);
88
89 // Keep running (or use signal handler in future)
90 while (1) {
91 sleep(1);
92 }
93
94 mg_stop(ctx);
95}
#define free
Definition civetweb.c:1542
CIVETWEB_API void mg_stop(struct mg_context *ctx)
Definition civetweb.c:20346
CIVETWEB_API const struct mg_request_info * mg_get_request_info(const struct mg_connection *conn)
Definition civetweb.c:3521
CIVETWEB_API struct mg_context * mg_start(const struct mg_callbacks *callbacks, void *user_data, const char **options)
Definition civetweb.c:21205
CIVETWEB_API int mg_printf(struct mg_connection *conn, const char *fmt,...)
Definition civetweb.c:7034
CIVETWEB_API void mg_set_request_handler(struct mg_context *ctx, const char *uri, mg_request_handler handler, void *cbdata)
Definition civetweb.c:14466
#define NULL
Definition gmacros.h:924
static int user_handler(struct mg_connection *conn, void *cbdata)
HTTP request handler for user lookup.
Definition server.c:26
void start_http_server(int port)
Starts an HTTP server on the specified port.
Definition server.c:59
Declaration of the HTTP server functions.
Structure representing a GitHub user.
Definition user.h:9
const char * name
Full name.
Definition user.h:11
const char * login
GitHub username.
Definition user.h:10
const char * location
User's location.
Definition user.h:13
const char * company
Company name.
Definition user.h:12
struct mg_callbacks callbacks
Definition civetweb.c:2432
const char * local_uri
Definition civetweb.h:157
User fetch_github_user(const char *username)
Fetch a GitHub user's information via the GitHub API.
Definition user.c:130
#define printf