-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendheader.cpp
More file actions
55 lines (47 loc) · 1.59 KB
/
sendheader.cpp
File metadata and controls
55 lines (47 loc) · 1.59 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
#include"sendheader.h"
using namespace std;
// send header constructor
SendHeader::SendHeader(int client, string fileName1, string pagestate1) :Header(client) {
// there was and error, fileName = fileName is and invalid expression, that Cpp will ignore it.
fileName = fileName1;
pagestate = pagestate1;
// call the get type function, and the type will be saved
get_HeadType(fileName);
}
const void SendHeader::get_HeadType(string fileName) {
// decide the head type
if (fileName != "") {
string fileType = fileName.substr(fileName.find_last_of('.') + 1);
if (fileType == "html") { HeadType = "text/html"; }
else if (fileType == "css") { HeadType = "text/css"; }
else if (fileType == "jpg") { HeadType = "image/jpg"; }
else if (fileType == "png") { HeadType = "image/png"; }
else if (fileType == "js") { HeadType = "application/x-javascript"; }
}
}
void SendHeader::sendheader() {
// 1.not supported/404/200
// 2.what filetype is it
// 3.send
// send pagestate
char buff[1024];
if (pagestate == "OK") {
strcpy_s(buff, "HTTP/1.1 200 OK\r\n");
}
else if (pagestate.c_str() == "NOTFOUND") {
strcpy_s(buff, "HTTP/1.1 404 NOT FOUND\r\n");
}
else if (pagestate.c_str() == "UNSUPPORTED") {
strcpy_s(buff, "HTTP/1.1 501 Method Not Implemented\r\n");
}
send(client, buff, strlen(buff), 0);
// send server name
strcpy_s(buff, "Server: tinyhttp/0.1\r\n");
send(client, buff, strlen(buff), 0);
// send content type
sprintf_s(buff, "Content-Type:%s\r\n", HeadType.c_str());
send(client, buff, strlen(buff), 0);
// send end
strcpy_s(buff, "\r\n");
send(client, buff, strlen(buff), 0);
}