-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_response.c
More file actions
90 lines (77 loc) · 2.61 KB
/
http_response.c
File metadata and controls
90 lines (77 loc) · 2.61 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
// File: http_response.c
// Created June 9, 2014
// Michael Baptist - mbaptist@ucsc.edu
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <dirent.h>
#include <sys/stat.h>
// comment this out to turn on debug prints.
//#define NDEBUG NDEBUG
#include "http_response.h"
#include "utils.h"
#define SUCCESS 0
#define FAILURE 1
void forbidden_response(char *buf, size_t buflen) {
char buffer[buflen];
bzero(buffer, buflen);
strncpy(buffer, "HTTP/1.1 403 Forbidden\r\n", 24);
strncat(buffer, "Content-type: text/html\n\n", 25);
strncat(buffer, "<html>\n", 7);
strncat(buffer, " <body>\n", 8);
strncat(buffer, " <h1>403 Forbidden</h1>\n", 25);
strncat(buffer, " <p>Access to this site is denied.</p>\n", 36);
strncat(buffer, " <body>\n", 8);
strncat(buffer, "<html>\n", 7);
strncat(buffer, "\r\n", 2);
strncat(buffer, "\r\n", 2);
memcpy(buf, buffer, buflen);
}
void unimplmented_response(char *buf, size_t buflen) {
char buffer[buflen];
bzero(buffer, buflen);
strncpy(buffer, "HTTP/1.1 501 Not Implemented\r\n", 30);
strncat(buffer, "Content-type: text/html\n\n", 25);
strncat(buffer, "<html>\n", 7);
strncat(buffer, " <body>\n", 8);
strncat(buffer, " <h1>501 Not Implemented</h1>\n", 31);
strncat(buffer, " <p>The HTTP request you made is not implemented.</p>\n", 55);
strncat(buffer, " <body>\n", 8);
strncat(buffer, "<html>\n", 7);
strncat(buffer, "\r\n", 2);
strncat(buffer, "\r\n", 2);
memcpy(buf, buffer, buflen);
}
void internal_error(char *buf, size_t buflen) {
char buffer[buflen];
bzero(buffer, buflen);
strncpy(buffer, "HTTP/1.1 500 Internal Server Error\r\n", 36);
strncat(buffer, "Content-type: text/html\n\n", 25);
strncat(buffer, "<html>\n", 7);
strncat(buffer, " <body>\n", 8);
strncat(buffer, " <h1>500 Internal Server Error</h1>\n", 37);
strncat(buffer, " <p>The proxy server had an error and the connection was aborted.</p>\n", 71);
strncat(buffer, " <body>\n", 8);
strncat(buffer, "<html>\n", 7);
strncat(buffer, "\r\n", 2);
strncat(buffer, "\r\n", 2);
memcpy(buf, buffer, buflen);
}
// returns a http response
char *http_response(const uint status) {
char *buffer = calloc(1, 1024);
if (403 == status) {
forbidden_response(buffer, 1024);
return buffer;
} else if (501 == status) {
unimplmented_response(buffer, 1024);
return buffer;
} else if (500 == status) {
internal_error(buffer, 1024);
return buffer;
}
return NULL;
}