-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.cpp
75 lines (73 loc) · 1.39 KB
/
Util.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
72
73
74
75
#include "Util.h"
namespace Util
{
//获取请求的绝对路径
string ConstructPath(const string &path)
{
string webHome="/home/zhangxiao/zxtest/myhttpd/httpservermuduo";
if(path=="/")
{
return webHome+"/index.html";
}
else
{
return webHome+path;
}
}
//获取文件扩展名
string GetExtent(const string &path)
{
int i;
for(i=path.size()-1;;--i)
{
if(path[i]=='.')
break;
}
return string(path.begin()+i+1,path.end());
}
//获取content-Type
void GetContentType(const string &tmpExtension,
string &contentType)
{
ifstream mimeFile("mime.types");
string line;
while(NULL!=getline(mimeFile,line))
{
if(line[0]!='#')
{
stringstream lineStream(line);
contentType.clear();
lineStream>>contentType;
vector<string>extensions;
string extension;
while(lineStream>>extension)
{
extensions.push_back(extension);
}
for(int i=0;i<extensions.size();++i)
{
if(tmpExtension==extensions[i])
{
mimeFile.close();
return ;
}
}
}
}
//如果都不匹配就默认为text/plain
contentType="text/plain";
mimeFile.close();
}
//获取Content
string GetContent(const string &fileName)
{
std::ifstream fin(fileName, std::ios::in | std::ios::binary);
if(fin.fail())
{
return string("");
}
std::ostringstream oss;
oss << fin.rdbuf();
return std::string(oss.str());
}
}