| 1 |
#include "WebServer.h" |
| 2 |
#include "../../String/StringInput.h" |
| 3 |
#include "../../System/System.h" |
| 4 |
#include "../../DateTime/DateTime.h" |
| 5 |
#include "../../System/SystemDefine.h" |
| 6 |
|
| 7 |
WebServer::WebServer(ConfigInput *iConfigInput) |
| 8 |
{ |
| 9 |
commandoList = new StringLinkedList(); |
| 10 |
|
| 11 |
commandoList->addLast("TRACE"); |
| 12 |
commandoList->addLast("trace"); |
| 13 |
commandoList->addLast("OPTIONS"); |
| 14 |
commandoList->addLast("options"); |
| 15 |
commandoList->addLast("PUT"); |
| 16 |
commandoList->addLast("put"); |
| 17 |
commandoList->addLast("DELETE"); |
| 18 |
commandoList->addLast("delete"); |
| 19 |
commandoList->addLast("POST"); |
| 20 |
commandoList->addLast("post"); |
| 21 |
|
| 22 |
configInput=iConfigInput; |
| 23 |
header = new Header(configInput);
|
| 24 |
directory = new Directory(header);
|
| 25 |
binaryInput = new BinaryInput();
|
| 26 |
homePageHandler = new HomePageHandler(header);
|
| 27 |
} |
| 28 |
|
| 29 |
WebServer::~WebServer() |
| 30 |
{
|
| 31 |
if (commandoList!=null)
|
| 32 |
{ |
| 33 |
delete commandoList; |
| 34 |
}
|
| 35 |
|
| 36 |
if (directory!=null)
|
| 37 |
{
|
| 38 |
delete directory;
|
| 39 |
}
|
| 40 |
|
| 41 |
if (header!=null)
|
| 42 |
{
|
| 43 |
delete header;
|
| 44 |
}
|
| 45 |
|
| 46 |
if (binaryInput!=null)
|
| 47 |
{
|
| 48 |
delete binaryInput;
|
| 49 |
}
|
| 50 |
|
| 51 |
if (homePageHandler!=null)
|
| 52 |
{
|
| 53 |
delete homePageHandler;
|
| 54 |
}
|
| 55 |
} |
| 56 |
|
| 57 |
bool WebServer::init(PacketInfo *packetInfo) |
| 58 |
{ |
| 59 |
int socket=packetInfo->socket;
|
| 60 |
bool connectionStatus=true; |
| 61 |
String request=packetInfo->request;
|
| 62 |
String filePath=packetInfo->filePath;
|
| 63 |
String protocolType=packetInfo->protocolType;
|
| 64 |
header->socket=socket;
|
| 65 |
header->protocolType->setString(protocolType);
|
| 66 |
String rootPath=configInput->getData("server","rootPath");
|
| 67 |
header->filePath->setString(rootPath + filePath);
|
| 68 |
header->filePathWithoutRootPath->setString(filePath);
|
| 69 |
header->rootPath->setString(rootPath);
|
| 70 |
|
| 71 |
// rootPath+="/";
|
| 72 |
|
| 73 |
// System::println(rootPath + " kalle samst");
|
| 74 |
// System::println(header->filePath);
|
| 75 |
/*
|
| 76 |
if(!header->filePath->theFilePathIsNotUnder(rootPath))
|
| 77 |
{
|
| 78 |
return header->makeAndSendHeaderWithMessageForbidden();
|
| 79 |
}
|
| 80 |
*/
|
| 81 |
|
| 82 |
String fullFilePath=header->filePath;
|
| 83 |
|
| 84 |
int atLeast=0;
|
| 85 |
|
| 86 |
if (!request.isNull())
|
| 87 |
{
|
| 88 |
atLeast++;
|
| 89 |
}
|
| 90 |
|
| 91 |
if (!filePath.isNull())
|
| 92 |
{
|
| 93 |
atLeast++;
|
| 94 |
}
|
| 95 |
|
| 96 |
if (!protocolType.isNull())
|
| 97 |
{
|
| 98 |
atLeast++;
|
| 99 |
}
|
| 100 |
|
| 101 |
bool requestTrue=false; |
| 102 |
|
| 103 |
if (atLeast == 3) |
| 104 |
{ |
| 105 |
if ((request.equals("GET") || request.equals("get")) && (protocolType.equals("http/1.0") || protocolType.equals("HTTP/1.0"))) |
| 106 |
{ |
| 107 |
connectionStatus=getCommando(filePath, socket); |
| 108 |
requestTrue=true; |
| 109 |
} |
| 110 |
else |
| 111 |
{ |
| 112 |
if (request.equals("GET") || request.equals("get")) |
| 113 |
{
|
| 114 |
header->protocolType->setString("HTTP/1.1");
|
| 115 |
connectionStatus=getCommando(filePath, socket); |
| 116 |
requestTrue=true; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
if ((request.equals("HEAD") || request.equals("head")) && ((protocolType.equals("http/1.1") || protocolType.equals("HTTP/1.1") || protocolType.equals("http/1.0") || protocolType.equals("HTTP/1.0")))) |
| 121 |
{ |
| 122 |
connectionStatus=getCommando(filePath, socket); |
| 123 |
requestTrue=true; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
if (atLeast!=0) |
| 128 |
{ |
| 129 |
int commandoListSize=commandoList->size(); |
| 130 |
|
| 131 |
for ( int i=0; i<commandoListSize ; i++ ) |
| 132 |
{ |
| 133 |
if (request.equals(commandoList->getElement(i))) |
| 134 |
{ |
| 135 |
String status="501 Not Implemented";
|
| 136 |
connectionStatus=header->makeAndSendHeaderErrorMessage(status);
|
| 137 |
requestTrue=true; |
| 138 |
i=commandoListSize; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
if (requestTrue == false && atLeast != 0) |
| 144 |
{ |
| 145 |
String status="400 Bad Request"; |
| 146 |
connectionStatus=header->makeAndSendHeaderErrorMessage(status);
|
| 147 |
} |
| 148 |
|
| 149 |
if (atLeast==0) |
| 150 |
{ |
| 151 |
connectionStatus=false; |
| 152 |
} |
| 153 |
|
| 154 |
return connectionStatus; |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
bool WebServer::getCommando(String &filePath, int socket) |
| 159 |
{ |
| 160 |
bool connectionStatus=true; |
| 161 |
bool ct=false; |
| 162 |
|
| 163 |
if (!directory->check())
|
| 164 |
{
|
| 165 |
if (homePageHandler->check())
|
| 166 |
{
|
| 167 |
return true;
|
| 168 |
}
|
| 169 |
|
| 170 |
String contentType="text/plain"; |
| 171 |
|
| 172 |
if (filePath.isBackPartEqualTo(".html")) |
| 173 |
{ |
| 174 |
contentType.setString("text/html"); |
| 175 |
} |
| 176 |
|
| 177 |
if (filePath.isBackPartEqualTo(".jpg")) |
| 178 |
{ |
| 179 |
contentType.setString("image/jpeg"); |
| 180 |
} |
| 181 |
|
| 182 |
if (filePath.isBackPartEqualTo(".gif")) |
| 183 |
{ |
| 184 |
contentType.setString("image/gif"); |
| 185 |
} |
| 186 |
|
| 187 |
if (filePath.isBackPartEqualTo(".png")) |
| 188 |
{ |
| 189 |
contentType.setString("image/png"); |
| 190 |
} |
| 191 |
|
| 192 |
if (filePath.isBackPartEqualTo(".zip"))
|
| 193 |
{
|
| 194 |
contentType.setString("application/zip");
|
| 195 |
}
|
| 196 |
|
| 197 |
binaryInput->readFile(header->filePath);
|
| 198 |
|
| 199 |
if (binaryInput->getFileStatus() == FileExist) |
| 200 |
{ |
| 201 |
header->makeAndSendHeaderWithBinaryData(binaryInput, contentType);
|
| 202 |
} |
| 203 |
|
| 204 |
if (binaryInput->getFileStatus() == FileDoesNotExist) |
| 205 |
{
|
| 206 |
header->makeAndSendHeaderWithMessageNotFound();
|
| 207 |
} |
| 208 |
|
| 209 |
if (binaryInput->getFileStatus() == CouldNotReadFile) |
| 210 |
|
| 211 |
{ |
| 212 |
header->makeAndSendHeaderWithMessageForbidden();
|
| 213 |
} |
| 214 |
|
| 215 |
}
|
| 216 |
else
|
| 217 |
{
|
| 218 |
if (directory->readPermission)
|
| 219 |
{
|
| 220 |
connectionStatus=directory->init();
|
| 221 |
}
|
| 222 |
else
|
| 223 |
{
|
| 224 |
header->makeAndSendHeaderWithMessageForbidden();
|
| 225 |
}
|
| 226 |
}
|
| 227 |
|
| 228 |
return connectionStatus; |
| 229 |
} |
| 230 |
|
| 231 |
|