Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/ai-brain-core / httpd.cpp |
| Date | Not specified in source |
| Repository | Janady13/ai-brain-core |
| Revision | 365850e27004318edd582881225024026b118c2d |
| Branch | claude/agi-architecture-diagram-01GJ7CohfKvLb77yiKpHqd6d |
| Path or archive member | httpd.cpp |
| Original SHA-256 | 54f51dbf86242eb089e4efc3322312f62eb00e0b1fa1d447809cf260bf3544ce |
| Git blob SHA | 079fddd8cb775b526aef236b03c9f2cf935df6f2 |
| Reading-copy SHA-256 | 54f51dbf86242eb089e4efc3322312f62eb00e0b1fa1d447809cf260bf3544ce |
Recovered source transcript
Download complete source text ↓116 lines · 2.7 KB
// httpd.cpp - Lightweight HTTP Server for AGI Frontend (C++)
#include <iostream>
#include <string>
#include <cstring>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/stat.h>
constexpr int PORT = 8080;
constexpr int BUFFER_SIZE = 4096;
const char *HTTP_200 = "HTTP/1.1 200 OK\r\n";
const char *HTTP_404 = "HTTP/1.1 404 Not Found\r\nContent-Length: 9\r\n\r\nNot Found";
const char *CONTENT_HTML = "Content-Type: text/html\r\n";
void send_file(int client_fd, const char *filepath) {
int fd = open(filepath, O_RDONLY);
if (fd < 0) {
write(client_fd, HTTP_404, strlen(HTTP_404));
return;
}
struct stat st;
fstat(fd, &st);
char header[512];
snprintf(header, sizeof(header),
"%s%sContent-Length: %ld\r\n\r\n",
HTTP_200, CONTENT_HTML, st.st_size);
write(client_fd, header, strlen(header));
char buf[BUFFER_SIZE];
ssize_t n;
while ((n = read(fd, buf, sizeof(buf))) > 0) {
write(client_fd, buf, n);
}
close(fd);
}
void handle_request(int client_fd) {
char buffer[BUFFER_SIZE];
ssize_t bytes_read = read(client_fd, buffer, sizeof(buffer) - 1);
if (bytes_read <= 0) {
close(client_fd);
return;
}
buffer[bytes_read] = '\0';
/* Parse request line */
char method[16], path[256];
sscanf(buffer, "%s %s", method, path);
std::cout << "[" << method << "] " << path << std::endl;
/* Route requests */
if (strcmp(path, "/") == 0 || strcmp(path, "/index.html") == 0) {
send_file(client_fd, "frontend/index.html");
} else {
write(client_fd, HTTP_404, strlen(HTTP_404));
}
close(client_fd);
}
int main() {
int server_fd, client_fd;
struct sockaddr_in addr;
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
perror("socket");
return 1;
}
int opt = 1;
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("bind");
close(server_fd);
return 1;
}
if (listen(server_fd, 10) < 0) {
perror("listen");
close(server_fd);
return 1;
}
std::cout << "🧠 AGI Frontend Server running on http://localhost:" << PORT << std::endl;
std::cout << " Serving: frontend/index.html\n" << std::endl;
while (1) {
client_fd = accept(server_fd, NULL, NULL);
if (client_fd < 0) {
perror("accept");
continue;
}
handle_request(client_fd);
}
close(server_fd);
return 0;
}