Skip to content

Commit 74006d8

Browse files
committed
added nonblocking read
1 parent 7a243e6 commit 74006d8

File tree

7 files changed

+343
-12
lines changed

7 files changed

+343
-12
lines changed

Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
CC = gcc
22
DEFS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_SVID_SOURCE -D_POSIX_C_SOURCE=200809L
33

4-
CFLAGS = -Wall -g -std=c99 -pedantic $(DEFS)
5-
SERVEROBJECTS = server.o
4+
CFLAGS = -Wall -Wno-unused-function -g -std=c99 -pedantic $(DEFS)
5+
SERVEROBJECTS = server.o httpHeaderManager.o
66
.PHONY: all clean
77

88
all: clean httpc
@@ -13,7 +13,8 @@ httpc: $(SERVEROBJECTS)
1313
%.o: %.c
1414
$(CC) $(CFLAGS) -c -o $@ $<
1515

16-
server.o: server.c server.h
16+
httpHeaderManager.o: httpHeaderManager.c httpHeaderManager.h httpStatusCodes.h
17+
server.o: server.c server.h httpHeaderManager.o
1718

1819
clean:
1920
rm -rf *.o httpc

httpHeaderManager.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
4+
#include "httpHeaderManager.h"
5+
#include "httpStatusCodes.h"
6+
7+
void responseheaderToString(httpheader_t header, char *headerString)
8+
{
9+
// status code is not longer than 3 digits
10+
char *statuscode = calloc(4, sizeof(char));
11+
sprintf(statuscode, "%d", header.statuscode);
12+
13+
// getting the reason for a given status code
14+
const char *statusText = HttpStatus_reasonPhrase(header.statuscode);
15+
16+
int length = sizeof(header.methode) +
17+
sizeof(header.file) +
18+
sizeof(header.httpVersion) +
19+
sizeof(header.host) +
20+
sizeof(header.connection) +
21+
sizeof(header.user_agent) +
22+
sizeof(header.accept_encoding) +
23+
sizeof(header.content_length) +
24+
sizeof(header.content_type) +
25+
sizeof(header.date) +
26+
sizeof(header.last_modified) +
27+
sizeof(statuscode) +
28+
sizeof(statusText) +
29+
2;
30+
31+
headerString = realloc(headerString, length);
32+
33+
sprintf(headerString, "%s %s %s\r\n", header.methode, statuscode, statusText);
34+
//TODO: implement
35+
}

httpHeaderManager.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "httpStatusCodes.h"
2+
3+
typedef struct
4+
{
5+
char *methode;
6+
char *file;
7+
char *httpVersion;
8+
char *host;
9+
char *connection;
10+
char *user_agent;
11+
char *accept_encoding;
12+
char *content_length;
13+
char *content_type;
14+
char *date;
15+
char *last_modified;
16+
int statuscode;
17+
} httpheader_t;
18+
19+
void responseheaderToString(httpheader_t header, char *headerString);

httpStatusCodes.h

Lines changed: 260 additions & 0 deletions
Large diffs are not rendered by default.

httpc

4.63 KB
Binary file not shown.

server.c

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <sys/wait.h>
1212

1313
#include "server.h"
14+
#include "httpHeaderManager.h"
15+
#include "httpStatusCodes.h"
1416

1517
void handle_childfork(int singal)
1618
{
@@ -107,7 +109,7 @@ int validPort(char *port)
107109
* @param argv the argument vector
108110
* @param args the container holding the arguments for further processing
109111
*/
110-
void readArguments(int argc, char *argv[], serverarguments *args)
112+
void readArguments(int argc, char *argv[], serverarguments_t *args)
111113
{
112114
int portFlag = 0;
113115
int ch = -1;
@@ -152,7 +154,7 @@ void readArguments(int argc, char *argv[], serverarguments *args)
152154
* @param args the arguments of this programm
153155
* @return int the servers filedescriptor
154156
*/
155-
int startServer(serverarguments *args)
157+
int startServer(serverarguments_t *args)
156158
{
157159
struct addrinfo hints, *ai;
158160
memset(&hints, 0, sizeof hints);
@@ -204,27 +206,40 @@ char *readClientReqest(int clientFd, char *requestContent)
204206
{
205207
FILE *clientInput = fdopen(clientFd, "r");
206208

207-
//TODO: find a way its not blocking
208209
char line[1024];
209210
while ((fgets(line, sizeof(line), clientInput)) != 0)
210211
{
211212
int newLength = strlen(line) + strlen(requestContent) + 2;
212213
requestContent = realloc(requestContent, newLength);
213214
strcat(requestContent, line);
215+
if (strcmp("\r\n", line) == 0)
216+
{
217+
//done
218+
break;
219+
}
214220
}
215221

216-
//fgets(requestContent,sizeof(requestContent),clientInput);
217-
218222
fclose(clientInput);
219223
return requestContent;
220224
}
221225

226+
void parseHttpHeader(httpheader_t *parseHttpHeader)
227+
{
228+
//TODO: implement header parsing
229+
}
230+
222231
void processClientRequest(int clientFd)
223232
{
224233
char *requestContent = calloc(1024, sizeof(char));
225-
226234
requestContent = readClientReqest(clientFd, requestContent);
227-
printf("%s", requestContent);
235+
236+
httpheader_t httpheader = {.methode = "GET", .statuscode = 200};
237+
parseHttpHeader(&httpheader);
238+
239+
char *httpHeaderAsString = calloc(2, sizeof(char));
240+
responseheaderToString(httpheader, httpHeaderAsString);
241+
242+
//printf("%s", requestContent);
228243
fflush(stdout);
229244
}
230245

@@ -249,6 +264,7 @@ void handleNewClient(int clientFd)
249264
default:
250265
// parent tasks ...
251266
// nothing to do
267+
// handle in sigaction
252268
break;
253269
}
254270
}
@@ -284,7 +300,7 @@ void clientWaiting(int serverFd)
284300
int main(int argc, char *argv[])
285301
{
286302
signalRegistry();
287-
serverarguments args = {
303+
serverarguments_t args = {
288304
.port = DEFAULTPORT};
289305

290306
readArguments(argc, argv, &args);

server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
typedef struct
1111
{
1212
char *port;
13-
} serverarguments;
13+
} serverarguments_t;
1414

1515
void usage(void);
1616

0 commit comments

Comments
 (0)