Repository navigation
Expand file tree
/
Copy pathserver_io.c
More file actions
129 lines (111 loc) · 4.1 KB
/
Copy pathserver_io.c
File metadata and controls
129 lines (111 loc) · 4.1 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
#include <regex.h> // regex comparisons
#include <stdio.h> // printf
#include <string.h> // strerror
#include <unistd.h> // read, write
#include "server_io.h"
/**
* Reads a request from the given client socket.
* @param cs: The client socket to read from.
* @param request: The buffer to store the request.
* @param bfrSize: The size of the buffer.
*/
void read_request(int cs, char* request, size_t bfrSize)
{
char c;
int i = 0; // Puffer für ein eingelesenes Zeichen
while (read(cs, &c, 1) == 1 && c != '\n' && c != '\r' && i < bfrSize - 1)
request[i++] = c;
request[i] = '\0'; // String terminieren
}
/**
* Parses a http request into its components.
* @param request: The request to parse.
* @param method: The buffer to store the HTTP method.
* @param uri: The buffer to store the request URI.
* @param version: The buffer to store the HTTP version.
* @return 0 if the request was successfully parsed, 1 otherwise.
*/
int parse_request(const char* request, char* method, char* uri, char* version)
{
regex_t reg;
regmatch_t matches[NUM_MATCHES];
// capture groups
// 1: HTTP method
// 2: request URI
// 3: HTTP-Version
const char* pattern = "^([^ ]*)" // match first word without space -> method, e.g. GET
" " // match space
"([^ ]*)" // match request URI, only allow absolute path
" " // match space
"HTTP\\/([0-9]+\\.[0-9]+)"; // match HTTP-Version (HTTP/1*digit.1*digit), e.g. HTTP/1.1
// compile regex
int compResult = regcomp(®, pattern, REG_EXTENDED);
if (compResult != 0)
{
printf("Error %d", compResult);
return 1;
}
int result = regexec(®, request, NUM_MATCHES, matches, 0);
regfree(®);
if (result == REG_NOMATCH || result != 0)
{
return 1;
printf("No matches");
}
// Ensure null termination for each component
int len = matches[1].rm_eo - matches[1].rm_so;
snprintf(method, len + 1, "%s", request + matches[1].rm_so);
len = matches[2].rm_eo - matches[2].rm_so;
snprintf(uri, len + 1, "%s", request + matches[2].rm_so);
len = matches[3].rm_eo - matches[3].rm_so;
snprintf(version, len + 1, "%s", request + matches[3].rm_so);
return 0;
}
/**
* Sends a file to the client socket.
* @param cs: The client socket to send the file to.
* @param fd: The file descriptor of the file to send.
* @return 0 if the file was successfully sent, 1 otherwise.
*/
int send_file_buffered(int cs, int fd)
{
char buffer[BUFSIZE];
int bytesRead, bytesWritten;
while ((bytesRead = read(fd, buffer, BUFSIZE)) > 0)
{
bytesWritten = write(cs, (void*)buffer, bytesRead);
if (bytesWritten < 0)
return 1;
}
if (bytesRead < 0)
return 1;
return 0;
}
/**
* Sends a http header to the client socket.
* @param cs: The client socket to send the header to.
* @param statusCode: The status code of the header.
* @param reasonPhrase: The reason phrase of the header.
* @param headerFields: The header fields to append to the header.
* @param numHeader: The number of header fields.
* @return 0 if the header was successfully sent, 1 otherwise.
*/
int send_header(int cs, int statusCode, const char* reasonPhrase, char headerFields[][BUFSIZE_SMALL], int numHeader)
{
char header[BUFSIZE];
int total_len = 0;
// apply generic header fields
total_len += snprintf(header, BUFSIZE,
"%s %d %s\r\n"
"Server: %s\r\n",
HTTP_VERSION, statusCode, reasonPhrase, SERVER_NAME);
// append custom header fields
for (int i = 0; i < numHeader; i++)
{
total_len += snprintf(header + total_len, BUFSIZE - total_len, "%s\r\n", headerFields[i]);
}
// append newline
total_len += snprintf(header + total_len, BUFSIZE - total_len, "\r\n");
int result = write(cs, (void*)header, strlen(header));
return (result == -1 || result < strlen(header)) ? -1 : 0;
}