-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.cpp
70 lines (61 loc) · 1.96 KB
/
http.cpp
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
#include <iostream>
#include <fstream>
#include <cstdio>
#include <winsock2.h>
using namespace std;
#include "http.h"
#include "sockUtils.h"
//Static methode
int http::sendMessage(int csock, const char* message,int length,int flags){
return send(csock,message,length,flags);
}
int http::sendHeader(const char* property){
std::string str = string(property)+"\r\n";
return http::sendMessage(this->_csock(),str.c_str(),str.length(),0);
}
int http::sendData(const char* data, bool withLine){
std::string str = string(data);
if(withLine)
str += "\r\n";
return http::sendMessage(this->_csock(),str.c_str(),str.length(),0);
}
int http::sendBinaryData(const char* data, int length,bool withLine){
std::string str = string(data);
if(withLine)
str += "\r\n";
return http::sendMessage(this->_csock(),str.c_str(),length,0);
}
int http::sendDocument(const char* filename) {
//ouverture en lecture
string new_filename = this->m_serveur._documentRoot()+string(filename);
FILE* fichier = fopen(new_filename.c_str(),"rb");
try{
int MAX = 4096;
char buffer[MAX];
int write=0,written = 0,read=0,readen=0,lastcount=0;
//while(fgets(buffer,255,fichier)){
do{
memset((void*)buffer,0,MAX);
read=fread(buffer,sizeof(char),MAX,fichier);
readen+=read;
write = sendBinaryData(buffer,read,false);
written += write;
if(write==SOCKET_ERROR ){
throw "Fichier corrompu";
}
}
while(!feof(fichier));
printf("%d caractères lus\n",readen);
printf("%d caractères émis\n",written);
fclose(fichier);
}catch(const char* e){
throw e;
}
return 1;
}
string http::http_decode(string url){
size_t s = url.find("%20");
if(s!=std::string::npos)
return url.replace(s,3, " ");
return url;
}