-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcami.c
More file actions
195 lines (169 loc) · 5.37 KB
/
cami.c
File metadata and controls
195 lines (169 loc) · 5.37 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* --- 1. PLATFORM-SPECIFIC CONFIGURATION AND HEADERS --- */
#if defined(__AMIGA__) || defined(__amigaos__)
#include <proto/bsdsocket.h>
struct Library *SocketBase = NULL;
#define CLOSE_SOCKET(s) sclose(s)
#define MUTEX_LOCK()
#define MUTEX_UNLOCK()
#elif defined(_WIN32)
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
#define CLOSE_SOCKET(s) closesocket(s)
typedef int socklen_t;
CRITICAL_SECTION win_mutex;
#define MUTEX_LOCK() EnterCriticalSection(&win_mutex)
#define MUTEX_UNLOCK() LeaveCriticalSection(&win_mutex)
#else
/* macOS, Linux, BSD, s390x, Cosmopolitan */
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <pthread.h>
#define CLOSE_SOCKET(s) close(s)
static pthread_mutex_t posix_mutex = PTHREAD_MUTEX_INITIALIZER;
#define MUTEX_LOCK() pthread_mutex_lock(&posix_mutex)
#define MUTEX_UNLOCK() pthread_mutex_unlock(&posix_mutex)
#endif
/* Thread and type specifications */
#ifdef _WIN32
typedef HANDLE pthread_t;
#define THREAD_FUNC DWORD WINAPI
#else
#define THREAD_FUNC void*
#endif
/* --- 2. MEMORY POOL --- */
#define POOL_SIZE 64
#define BUFFER_SIZE 2048
typedef struct {
char data[BUFFER_SIZE];
int in_use;
} MemoryBuffer;
MemoryBuffer pool[POOL_SIZE];
MemoryBuffer* get_buffer() {
MUTEX_LOCK();
for (int i = 0; i < POOL_SIZE; i++) {
if (!pool[i].in_use) {
pool[i].in_use = 1;
MUTEX_UNLOCK();
return &pool[i];
}
}
MUTEX_UNLOCK();
return NULL;
}
void release_buffer(MemoryBuffer* buf) {
if (buf) {
MUTEX_LOCK();
buf->in_use = 0;
MUTEX_UNLOCK();
}
}
/* --- 3. JSON AND ANSWERS --- */
void build_json_response(char* buffer) {
const char* json_body = "{\"message\":\"Hello, World!\"}";
sprintf(buffer,
"HTTP/1.1 200 OK\r\n"
"Content-Type: application/json\r\n"
"Content-Length: %lu\r\n"
"Connection: close\r\n"
"\r\n"
"%s", (unsigned long)strlen(json_body), json_body);
}
void build_plaintext_response(char* buffer) {
const char* body = "Hello, World!";
sprintf(buffer,
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: %lu\r\n"
"Connection: close\r\n"
"\r\n"
"%s", (unsigned long)strlen(body), body);
}
void build_db_response(char* buffer, int id, const char* random_text) {
char json_body[256];
sprintf(json_body, "{\"id\":%d,\"randomNumber\":\"%s\"}", id, random_text);
sprintf(buffer,
"HTTP/1.1 200 OK\r\n"
"Content-Type: application/json\r\n"
"Content-Length: %lu\r\n"
"Connection: close\r\n"
"\r\n"
"%s", (unsigned long)strlen(json_body), json_body);
}
/* --- 4. CLIENT HANDLING --- */
THREAD_FUNC handle_client(void *arg) {
long client_fd = (long)arg;
char request_buf[1024];
// Luetaan pyyntö
int received = recv(client_fd, request_buf, sizeof(request_buf) - 1, 0);
MemoryBuffer* buf = get_buffer();
if (buf && received > 0) {
request_buf[received] = '\0';
if (strstr(request_buf, "GET /db")) {
build_db_response(buf->data, 123, "AmigaMagic");
} else if (strstr(request_buf, "GET /json")) {
build_json_response(buf->data);
} else {
build_plaintext_response(buf->data);
}
send(client_fd, buf->data, (int)strlen(buf->data), 0);
release_buffer(buf);
} else if (!buf) {
const char *error = "HTTP/1.1 503 Service Unavailable\r\n\r\n";
send(client_fd, error, (int)strlen(error), 0);
}
CLOSE_SOCKET(client_fd);
#ifdef _WIN32
return 0;
#else
return NULL;
#endif
}
/* --- 5. MAIN PROGRAM --- */
int main() {
memset(pool, 0, sizeof(pool));
#if defined(_WIN32)
WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData);
InitializeCriticalSection(&win_mutex);
#elif defined(__AMIGA__) || defined(__amigaos__)
SocketBase = OpenLibrary("bsdsocket.library", 4);
#endif
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) return 1;
int opt = 1;
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(8080);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(server_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) return 1;
listen(server_fd, 128);
printf("Universal JSON Server running on port 8080...\n");
while(1) {
struct sockaddr_in client_addr;
socklen_t addr_len = sizeof(client_addr);
long client_fd = (long)accept(server_fd, (struct sockaddr *)&client_addr, &addr_len);
if (client_fd >= 0) {
#ifdef _WIN32
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)handle_client, (void*)client_fd, 0, NULL);
#elif defined(__AMIGA__) || defined(__amigaos__)
handle_client((void*)client_fd);
#else
pthread_t thread;
if (pthread_create(&thread, NULL, handle_client, (void*)client_fd) == 0) {
pthread_detach(thread);
} else {
CLOSE_SOCKET(client_fd);
}
#endif
}
}
return 0;
}