-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserveur.cpp
71 lines (63 loc) · 1.98 KB
/
serveur.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
71
#include <iostream>
#include <string.h>
#include <cstdlib>
#include <ctype.h> //tolower
using namespace std;
#include "serveur.h"
#include "sockUtils.h"
int serveur::sizeDocument(const char* filename){
int length = 0;
string new_filename = _documentRoot()+string(filename);
ifstream fichier(new_filename.c_str(), ios::in);
if(fichier.is_open()){
fichier.seekg(0,fichier.end);
length = fichier.tellg();
fichier.seekg (0, fichier.beg);
fichier.close();
}
return length;
}
bool serveur::findDocument(const char* filename){
string new_filename = _documentRoot()+string(filename);
ifstream fichier(new_filename.c_str(), ios::in);
bool found = false;
if(fichier){
found = true;
fichier.close();
}
return found;
}
string serveur::getMimeType(const char* completefilename){
ifstream fichier((documentRoot+string("mimes.txt")).c_str(),ios::in);
if(!fichier.is_open())
throw "Le fichier mime est manquant.";
char** ext = NULL;
int nb = 0;
if(string(completefilename).find(".") != std::string::npos){
ext = explode(".",(char*)completefilename,&nb);
}
//dernier élément après le '.'
nb = nb > 0 ? nb-1 : 0;
cout << "Ext : " << ext[nb] << endl;
if(strlen(ext[nb])==0)
throw "Nom de fichier incorrect.";
for(int i=0; i<strlen(ext[nb]);i++)
ext[nb][i] = tolower(ext[nb][i]);
string line, mime = "";
bool found = false;
while(getline(fichier,line) && !found){
if(line.find("=") != std::string::npos){
int nb2 = 0;
char** words = explode("=",(char*)line.c_str(),&nb2);
if(nb2==2){
if(strncmp(ext[nb],words[0],strlen(ext[nb]))==0){
found = true;
mime = string(words[1]);
}
}
free(words);
}
}
free(ext);
return mime;
}