-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTPServer.h
149 lines (125 loc) · 3.61 KB
/
HTTPServer.h
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <time.h>
const int BUFF_SIZE = 1024;
const int HBUFF_SIZE = 256;
int respond(int newSock, char rbuffer[BUFF_SIZE], double delay, int verbose, char HOMEPAGE[], char *imgData, int imgSize)
{
// Send homepage if requested
if( !strncmp("GET / HTTP/1.1", rbuffer, strlen("GET / HTTP/1.1")) )
{
if(verbose)
printf("Sending home page... ");
// Initialize response
// Read from HTML file
char *buffer = (char *)malloc(HBUFF_SIZE*sizeof(char));
char body[1024] = "";
FILE *hptr = fopen(HOMEPAGE, "r");
size_t bytes;
while( (bytes = fread(buffer, 1, HBUFF_SIZE, hptr)) > 0)
{
// Append to body
strncat(body, buffer, bytes);
}
fclose(hptr);
free(buffer);
// Set delay in web page
sprintf(body, body, delay, (int)(delay*1000));
char header[128];
sprintf(header, "HTTP/1.1 200 OK\nContent-Type: text/html\nContent-Length: %ld\n\n", strlen(body));
// Send HTTP response header
send(newSock, header, strlen(header), 0);
// Send response body
send(newSock, body, strlen(body), 0);
if(verbose)
printf("[done]\n");
return 1;
}
else if( !strncmp("GET /scr.jpg", rbuffer, strlen("GET /scr.jpg")) )
{
if(verbose)
printf("Sending scr.jpg... ");
// Initialize response
char header[] = "HTTP/1.1 200 OK\nContent-Type: image/jpeg\n\n";
// Send HTTP response header
send(newSock, header, strlen(header), 0);
// Send response body image data
if(verbose)
printf("(%d B)", imgSize);
send(newSock, imgData, imgSize, 0);
// Free image data after sending
free(imgData);
if(verbose)
printf("[done]\n");
return 0;
}
}
void server(int port, double delay, int verbose, char HOMEPAGE[], char *imgData, int imgSize)
{
// Initialize socket
int servSock = socket(AF_INET, SOCK_STREAM, 0);
int option = 1;
setsockopt(servSock, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
if(servSock == -1)
{
perror("Socket could not be created.\n");
exit(EXIT_FAILURE);
}
// Define server address
struct sockaddr_in servAddr;
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(port);
servAddr.sin_addr.s_addr = INADDR_ANY;
// Bind socket to server address
if(bind(servSock, &servAddr, sizeof(servAddr)) == -1)
{
perror("Could not bind socket.\n");
exit(EXIT_FAILURE);
}
// Listen to given port
if(listen(servSock, 1) == -1)
{
perror("Could not listen to given port.\n");
return(EXIT_FAILURE);
}
if(verbose)
printf("Listening to port %d... ", port);
// Initialize and accept a new incoming connection
struct sockaddr_in cliAddr;
int cliAddrLen = sizeof(cliAddr);
int newSock = accept(servSock, &cliAddr, &cliAddrLen);
char *cliIP = inet_ntoa(cliAddr.sin_addr);
if(newSock == -1)
{
perror("Could not accept incoming connection.\n");
return(EXIT_FAILURE);
}
if(verbose)
printf("[connected to %s]\n",cliIP);
// Recieve request text
char *rbuffer = (char *)malloc(BUFF_SIZE*sizeof(char));
read(newSock, rbuffer, BUFF_SIZE - 1);
rbuffer[BUFF_SIZE - 1] = '\0';
// Send appropriate response
if(respond(newSock, rbuffer, delay, verbose, HOMEPAGE, imgData, imgSize))
{
// Free and rellocate read buffer memory
free(rbuffer);
rbuffer = (char *)malloc(BUFF_SIZE*sizeof(char));
// Send screenshot image if requested
read(newSock, rbuffer, BUFF_SIZE - 1);
rbuffer[BUFF_SIZE - 1] = '\0';
// Send appropriate response
respond(newSock, rbuffer, delay, verbose, HOMEPAGE, imgData, imgSize);
}
// Free read buffer memory
free(rbuffer);
// Close all conections
close(newSock);
close(servSock);
}