forked from RealOkabe/CS631-Group-Assignment-SWS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.c
74 lines (61 loc) · 1.9 KB
/
logger.c
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
#include "logger.h"
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "structures.h"
int writelog(int fd, struct HTTP_REQUEST request, struct HTTP_RESPONSE response,
char* requestip) {
FILE* fp;
if ((fp = fdopen(fd, "a")) == NULL) {
return (-1);
}
char* writedata;
if ((writedata = malloc(PATH_MAX * sizeof(char*))) == NULL) {
return (-1);
}
(void)strncpy(writedata, requestip, strlen(requestip));
(void)strncat(writedata, " ", 1);
struct tm* gmttime;
const time_t requesttime = request.time_request_made;
if ((gmttime = gmtime(&requesttime)) == NULL) {
return (-1);
}
char* finaltime;
if ((finaltime = malloc(DATE_MAX_LEN * sizeof(char*))) == NULL) {
return (-1);
}
if ((strftime(finaltime, DATE_MAX_LEN, "%FT%TZ", gmttime)) == 0) {
return (-1);
}
(void)strncat(writedata, finaltime, strlen(finaltime));
(void)strncat(writedata, " \"", 2);
(void)strncat(writedata, request.verb, strlen(request.verb));
(void)strncat(writedata, "\" ", 2);
char* status_code;
if ((status_code = malloc(INT_MAX * sizeof(char*))) == 0) {
return (-1);
}
if ((sprintf(status_code, "%d", response.status_code)) == -1) {
return (-1);
}
(void)strncat(writedata, status_code, strlen(status_code));
char* content_length;
if ((content_length = malloc(INT_MAX * sizeof(char*))) == 0) {
return (-1);
}
if ((sprintf(content_length, "%ld", response.content_length)) == -1) {
return (-1);
}
(void)strncat(writedata, " ", 1);
(void)strncat(writedata, content_length, strlen(content_length));
(void)strncat(writedata, "\n", 1);
if ((fprintf(fp, writedata)) == -1) {
return (-1);
}
(void)fclose(fp);
return (0);
}