00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "HTML.h"
00026
00027
00028 namespace cmlabs {
00029
00030
00031
00032
00033
00034
00035 HTMLPage::HTMLPage() {
00036
00037 autoRefresh = -1;
00038 useLocalCopy = noCaching = movedPermanently = false;
00039 title = "Error";
00040 body = "Page not found!";
00041 bodyBGColor = "#FFFFFF";
00042 bodyFGColor = "#111111";
00043 data = NULL;
00044 datalength = -1;
00045 rawData = NULL;
00046 rawDataLength = 0;
00047 keep_alive = true;
00048 }
00049
00050 HTMLPage::HTMLPage(const JString& title, const JString& bodyhtml) {
00051 autoRefresh = -1;
00052 useLocalCopy = noCaching = movedPermanently = false;
00053 this->title = title;
00054 this->body = bodyhtml;
00055 data = NULL;
00056 datalength = -1;
00057 rawData = NULL;
00058 rawDataLength = 0;
00059 keep_alive = true;
00060 }
00061
00062 HTMLPage::HTMLPage(char* rawdata, int len, const JString& type) {
00063
00064 autoRefresh = -1;
00065 useLocalCopy = noCaching = movedPermanently = false;
00066 title = "Error";
00067 body = "Page not found!";
00068 bodyBGColor = "#FFFFFF";
00069 bodyFGColor = "#111111";
00070 data = NULL;
00071 datalength = -1;
00072 rawData = rawdata;
00073 rawDataLength = len;
00074 rawDataType = type;
00075 keep_alive = true;
00076 }
00077
00078 HTMLPage::HTMLPage(Bitmap* bitmap) {
00079
00080 autoRefresh = -1;
00081 useLocalCopy = noCaching = movedPermanently = false;
00082 title = "Error";
00083 body = "Page not found!";
00084 bodyBGColor = "#FFFFFF";
00085 bodyFGColor = "#111111";
00086 data = NULL;
00087 datalength = -1;
00088 if (bitmap != NULL) {
00089 rawData = bitmap->toBitmapFileFormat(rawDataLength);
00090
00091
00092 rawDataType = ".bmp";
00093 }
00094 keep_alive = true;
00095 }
00096
00097 HTMLPage::HTMLPage(const JString& filename) {
00098
00099 useLocalCopy = noCaching = movedPermanently = false;
00100 data = NULL;
00101 datalength = -1;
00102 rawData = NULL;
00103 rawDataLength = 0;
00104 autoRefresh = -1;
00105 htmlFilename = filename;
00106 keep_alive = true;
00107 }
00108
00109 HTMLPage::HTMLPage(XMLNode* node) {
00110 useLocalCopy = noCaching = movedPermanently = false;
00111 data = NULL;
00112 datalength = -1;
00113 rawData = NULL;
00114 rawDataLength = 0;
00115 autoRefresh = -1;
00116 fromXML(node);
00117 keep_alive = true;
00118 }
00119
00120 HTMLPage::~HTMLPage() {
00121 if (data != NULL)
00122 delete [] data;
00123 if (rawData != NULL)
00124 delete [] rawData;
00125 }
00126
00127
00128 unsigned long HTMLPage::getPayloadSize() const {
00129 return rootdir.getPayloadSize() +
00130 htmlFilename.getPayloadSize() +
00131 title.getPayloadSize() +
00132 body.getPayloadSize() +
00133 location.getPayloadSize() +
00134 everything.getPayloadSize() +
00135 frameset.getPayloadSize() +
00136 xmlData.getPayloadSize() +
00137 rawDataType.getPayloadSize() +
00138 autoRefreshPage.getPayloadSize() +
00139 connectionType.getPayloadSize() +
00140 bodyBGColor.getPayloadSize() +
00141 bodyFGColor.getPayloadSize() +
00142 datalength + rawDataLength;
00143 }
00144
00145 Object* HTMLPage::clone() const {
00146 HTMLPage* page = new HTMLPage();
00147 *page = *this;
00148 page->data = NULL;
00149 page->datalength = -1;
00150 return (Object*) page;
00151 }
00152
00153 bool HTMLPage::fromXML(XMLNode* node) {
00154
00155 if ( (node == NULL) || (!node->getTag().equalsIgnoreCase("htmlpage")) )
00156 return false;
00157
00158 XMLNode* xmlNode = node->getChildNode("page");
00159 if (xmlNode != NULL) {
00160 rootdir = node->getAttribute("rootdir");
00161 htmlFilename = node->getAttribute("htmlfilename");
00162 title = node->getAttribute("title");
00163 autoRefreshPage = node->getAttribute("autorefreshpage");
00164 bodyBGColor = node->getAttribute("bodybgcolor");
00165 bodyFGColor = node->getAttribute("bodyfgcolor");
00166
00167 autoRefresh = node->getAttribute("autorefresh").toBool();
00168 noCaching = node->getAttribute("nocaching").toBool();
00169 useLocalCopy = node->getAttribute("uselocalcopy").toBool();
00170 }
00171
00172 if ( (xmlNode = node->getChildNode("body")) != NULL )
00173 body = xmlNode->getTextContent();
00174
00175 if ( (xmlNode = node->getChildNode("everything")) != NULL )
00176 everything = xmlNode->getTextContent();
00177
00178 if ( (xmlNode = node->getChildNode("frameset")) != NULL )
00179 frameset = xmlNode->getTextContent();
00180
00181 return true;
00182
00183
00184 }
00185
00186 JString HTMLPage::toXML() {
00187
00188 JString xml = JString::format("<page rootdir=\"%s\" htmlfilename=\"%s\" title=\"%s\" autorefresh=\"%s\" autorefreshpage=\"%s\" nocaching=\"%s\" uselocalcopy=\"%s\" bodyfgcolor=\"%s\" bodybgcolor=\"%s\" />\n",
00189 (char*) rootdir.xmlStringEncode(), (char*) htmlFilename.xmlStringEncode(), (char*) title.xmlStringEncode(), (char*) JString(autoRefresh), (char*) autoRefreshPage.xmlStringEncode(),
00190 (char*) JString(noCaching), (char*) JString(useLocalCopy), (char*) bodyBGColor.xmlStringEncode(), (char*) bodyFGColor.xmlStringEncode());
00191
00192 xml += JString::format("<body>%s</body>\n", (char*) body.xmlStringEncode());
00193 xml += JString::format("<everything>%s</everything>\n", (char*) everything.xmlStringEncode());
00194 xml += JString::format("<frameset>%s</frameset>\n", (char*) frameset.xmlStringEncode());
00195
00196 return JString("<htmlpage>\n") + xml.indentXML() + JString("</htmlpage>\n");
00197 }
00198
00199 bool HTMLPage::setMovedPermanently(const JString& location) {
00200 if (location.length() == 0)
00201 return false;
00202 this->location = location;
00203 this->movedPermanently = true;
00204 return true;
00205 }
00206
00207 bool HTMLPage::generateHTML() {
00208
00209 if (useLocalCopy)
00210 return generateUseLocalCopyPage();
00211
00212 if (movedPermanently)
00213 return generateMovedPermanentlyPage();
00214
00215 if (xmlData.length() > 0)
00216 return generateHTMLfromXML(xmlData);
00217
00218 if (rawData != NULL)
00219 return generateHTMLfromRawData();
00220
00221 if (htmlFilename.length() > 0)
00222 return generateHTMLfromFile(htmlFilename);
00223
00224 JString text;
00225
00226 if (everything.length() > 0) {
00227 text = everything;
00228 if ((noCaching) && (!text.containsIgnoreCase("no-cache")))
00229 text.replace("</head>", "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n</head>");
00230 }
00231 else {
00232
00233 text += "<HTML>\n";
00234
00235 text += JString::format("<HEAD>\n<TITLE>%s</TITLE>\n", (char*) title);
00236 if (noCaching)
00237 text += "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n";
00238 if (autoRefresh >= 0)
00239 text += JString("<META http-equiv=\"Refresh\" content=\"") + JString(autoRefresh) + JString(";URL=\"") + autoRefreshPage + "\">\n\n";
00240 text += "</HEAD>\n\n";
00241
00242 if (frameset.length() > 0) {
00243 text += frameset;
00244 }
00245 else {
00246 text += JString("<BODY STYLE=\"\" BGCOLOR=\"") + bodyBGColor + JString("\" TEXT=\"") + bodyFGColor + JString("\">");
00247 text += body;
00248 text += "</BODY>\n";
00249 }
00250
00251 text += "</HTML>\n";
00252 }
00253
00254 JString total = generateHeader(text.length());
00255
00256 total += text;
00257
00258 if (data != NULL)
00259 delete [] data;
00260 data = total.getCharCopy(datalength);
00261
00262 if (DEBUGLEVEL(STATUS)) {
00263 printf("HTML Internal Page '%s' generated %d bytes of data\n", (char*) title, datalength);
00264 }
00265 return (datalength > 0);
00266 }
00267
00268
00269 JString HTMLPage::generateHeader(int len) {
00270
00271 JTime t;
00272
00273 JString text = "HTTP/1.1 200 OK";
00274 text.addCRLF();
00275 text += JString("Date: ") + t.toHTTP();
00276 text.addCRLF();
00277 text += "Server: CMLabs Server/1.0";
00278 text.addCRLF();
00279 text += JString("Last-Modified: ") + t.toHTTP();
00280 text.addCRLF();
00281
00282
00283
00284
00285 text += JString("Content-Length: ") + JString(len);
00286 text.addCRLF();
00287 if (keep_alive)
00288 text += "Connection: Keep-Alive";
00289 else
00290 text += "Connection: close";
00291 text.addCRLF();
00292 text += "Content-Type: text/html";
00293 text.addCRLF();
00294 if (noCaching) {
00295 text += "Cache-Control: No-Cache";
00296 text.addCRLF();
00297 }
00298
00299 text.addCRLF();
00300
00301 return text;
00302 }
00303
00304 bool HTMLPage::generateUseLocalCopyPage() {
00305
00306
00307
00308
00309
00310 JTime t;
00311
00312 JString text = "HTTP/1.1 304 Use local copy";
00313 text.addCRLF();
00314 text += JString("Date: ") + t.toHTTP();
00315 text.addCRLF();
00316 text += "Server: CMLabs Server/1.0";
00317 text.addCRLF();
00318
00319 text.addCRLF();
00320
00321 if (data != NULL)
00322 delete [] data;
00323 data = text.getCharCopy(datalength);
00324
00325 return (datalength > 0);
00326 }
00327
00328 bool HTMLPage::generateMovedPermanentlyPage() {
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339 JString text = JString::format("<html><head>\
00340 <title>301MovedPermanently</title>\
00341 </head><body>\
00342 <h1>MovedPermanently</h1>\
00343 <p>Thedocumenthasmoved<ahref=\"%s\">here</a>.</p>\
00344 </body></html>", (char*) location);
00345
00346 JTime t;
00347
00348 JString total = "HTTP/1.1 301 Moved Permanently";
00349 total.addCRLF();
00350 total += JString("Date: ") + t.toHTTP();
00351 total.addCRLF();
00352 total += "Server: CMLabs Server/1.0";
00353 total.addCRLF();
00354 total += JString("Location: ") + location;
00355 total.addCRLF();
00356 total += JString("Content-Length: ") + JString(text.length());
00357 total.addCRLF();
00358 total += JString("Connection: close");
00359 total.addCRLF();
00360 total += "Content-Type: text/html";
00361 total.addCRLF();
00362
00363 total.addCRLF();
00364
00365 total += text;
00366
00367 if (data != NULL)
00368 delete [] data;
00369 data = total.getCharCopy(datalength);
00370 return (datalength > 0);
00371 }
00372
00373 bool HTMLPage::setXMLContent(const JString& xml) {
00374 xmlData = xml;
00375 return true;
00376 }
00377
00378 bool HTMLPage::generateHTMLfromXML(const JString& xml) {
00379
00380 JTime t;
00381 bool isBinary = false;
00382 bool useCached = false;
00383 JString contentType = "text/text";
00384 JString result = "HTTP/1.1 200 OK";
00385
00386 JString htmlText;
00387 if (xml.length() == 0) {
00388 htmlText = JString::format("<empty/>\n");
00389 data = htmlText.getCharCopy(datalength);
00390 }
00391 else {
00392 data = xml.getCharCopy(datalength);
00393
00394 }
00395
00396 if ((datalength <= 0) || (data == NULL))
00397 return false;
00398
00399 JString text = result;
00400 text.addCRLF();
00401 text += JString("Date: ") + t.toHTTP();
00402 text.addCRLF();
00403 text += "Server: CoreLibrary/0.9";
00404 text.addCRLF();
00405 text += JString("Last-Modified: ") + t.toHTTP();
00406 text.addCRLF();
00407 text += JString("Content-Length: ") + JString(datalength);
00408 text.addCRLF();
00409 if (keep_alive)
00410 text += "Connection: Keep-Alive";
00411 else
00412 text += "Connection: close";
00413 text.addCRLF();
00414 text += "Content-Type: ";
00415 text += contentType;
00416 text.addCRLF();
00417 text.addCRLF();
00418
00419 int headerlength;
00420 char* headerdata = text.getCharCopy(headerlength);
00421
00422 char* totaldata = new char[headerlength + datalength];
00423 memcpy(totaldata, headerdata, headerlength);
00424 memcpy(totaldata+headerlength, data, datalength);
00425
00426 delete [] data;
00427 delete [] headerdata;
00428
00429 data = totaldata;
00430 datalength = headerlength + datalength;
00431
00432 if (DEBUGLEVEL(STATUS)) {
00433 printf("HTML XML generated %d bytes of data\n", datalength);
00434 }
00435 return true;
00436 }
00437
00438
00439 bool HTMLPage::generateHTMLfromFile(const JString& filename) {
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462 JTime t;
00463 bool isBinary = false;
00464 bool useCached = false;
00465 JString contentType;
00466 JString result = "HTTP/1.1 200 OK";
00467
00468 if (filename.endsWith(".txt")) {
00469 isBinary = false;
00470 useCached = true;
00471 contentType = "text/text";
00472 }
00473 else if (filename.endsWith(".html")) {
00474 isBinary = false;
00475 useCached = true;
00476 contentType = "text/html";
00477 }
00478 else if (filename.endsWith(".css")) {
00479 isBinary = false;
00480 useCached = true;
00481 contentType = "text/css";
00482 }
00483 else if (filename.endsWith(".js")) {
00484 isBinary = false;
00485 useCached = true;
00486 contentType = "application/x-javascript";
00487 }
00488 else if (filename.endsWith(".gif")) {
00489 isBinary = true;
00490 useCached = true;
00491 contentType = "image/gif";
00492 }
00493 else if (filename.endsWith(".jpg")) {
00494 isBinary = true;
00495 useCached = true;
00496 contentType = "image/jpg";
00497 }
00498 else if (filename.endsWith(".jar")) {
00499 isBinary = true;
00500 useCached = true;
00501 contentType = "application/java-archive";
00502 }
00503 else if (filename.endsWith(".class")) {
00504 isBinary = true;
00505 useCached = true;
00506 contentType = "application/java-vm";
00507 }
00508 else if (filename.endsWith(".pdf")) {
00509 isBinary = true;
00510 useCached = true;
00511 contentType = "application/pdf";
00512 }
00513 else if ((filename.endsWith(".ai")) || (filename.endsWith(".eps")) || (filename.endsWith(".ps"))) {
00514 isBinary = true;
00515 useCached = true;
00516 contentType = "application/postscript";
00517 }
00518 else if ((filename.endsWith(".mid")) || (filename.endsWith(".midi"))) {
00519 isBinary = true;
00520 useCached = true;
00521 contentType = "audio/midi";
00522 }
00523 else if (filename.endsWith(".tar")) {
00524 isBinary = true;
00525 useCached = true;
00526 contentType = "application/x-tar";
00527 }
00528 else if (filename.endsWith(".zip")) {
00529 isBinary = true;
00530 useCached = true;
00531 contentType = "application/zip";
00532 }
00533 else if (filename.endsWith(".wav")) {
00534 isBinary = true;
00535 useCached = true;
00536 contentType = "audio/x-wav";
00537 }
00538 else if (filename.endsWith(".png")) {
00539 isBinary = true;
00540 useCached = true;
00541 contentType = "image/png";
00542 }
00543 else if (filename.endsWith(".bmp")) {
00544 isBinary = true;
00545 useCached = true;
00546 contentType = "image/bmp";
00547 contentType = "image/x-bmp";
00548 }
00549 else {
00550 isBinary = false;
00551 useCached = true;
00552 contentType = "text/html";
00553 }
00554
00555 JString htmlText;
00556
00557 readFile(filename, isBinary);
00558
00559 if (datalength <= 0) {
00560 htmlText = "<HTML>\n";
00561 htmlText += "<HEAD>\n<TITLE>";
00562 htmlText += "File not found: ";
00563 htmlText += filename;
00564 htmlText += "</TITLE>\n</HEAD>\n<BODY>\n";
00565 htmlText += "<H1>Sorry, the file ";
00566 htmlText += filename;
00567 htmlText += " was not found on the server</H1>";
00568 htmlText += "</BODY>\n";
00569 htmlText += "</HTML>\n";
00570 if (data != NULL)
00571 delete [] data;
00572 data = htmlText.getCharCopy(datalength);
00573 result = "HTTP/1.1 404 Object Not Found";
00574 }
00575
00576 if ((datalength <= 0) || (data == NULL))
00577 return false;
00578
00579 JString text = result;
00580 text.addCRLF();
00581 text += JString("Date: ") + t.toHTTP();
00582 text.addCRLF();
00583 text += "Server: CoreLibrary/0.9";
00584 text.addCRLF();
00585 text += JString("Last-Modified: ") + t.toHTTP();
00586 text.addCRLF();
00587
00588
00589
00590
00591 text += JString("Content-Length: ") + JString(datalength);
00592 text.addCRLF();
00593
00594
00595 if (keep_alive)
00596 text += "Connection: Keep-Alive";
00597 else
00598 text += "Connection: close";
00599 text.addCRLF();
00600 text += "Content-Type: ";
00601 text += contentType;
00602 text.addCRLF();
00603
00604 text.addCRLF();
00605
00606 int headerlength;
00607 char* headerdata = text.getCharCopy(headerlength);
00608
00609 char* totaldata = new char[headerlength + datalength];
00610 memcpy(totaldata, headerdata, headerlength);
00611 memcpy(totaldata+headerlength, data, datalength);
00612
00613 delete [] data;
00614 delete [] headerdata;
00615
00616 data = totaldata;
00617 datalength = headerlength + datalength;
00618
00619 if (DEBUGLEVEL(STATUS)) {
00620 printf("HTML File '%s' generated %d bytes of data\n", (char*) filename, datalength);
00621 }
00622 return true;
00623 }
00624
00625 bool HTMLPage::generateHTMLfromRawData() {
00626
00627 JTime t;
00628 bool isBinary = false;
00629 bool useCached = false;
00630 JString contentType;
00631 JString result = "HTTP/1.1 200 OK";
00632
00633 if (rawDataType.equalsIgnoreCase(".txt")) {
00634 isBinary = false;
00635 useCached = false;
00636 contentType = "text/text";
00637 }
00638 else if (rawDataType.equalsIgnoreCase(".html")) {
00639 isBinary = false;
00640 useCached = false;
00641 contentType = "text/html";
00642 }
00643 else if (rawDataType.equalsIgnoreCase(".js")) {
00644 isBinary = false;
00645 useCached = false;
00646 contentType = "application/x-javascript";
00647 }
00648 else if (rawDataType.equalsIgnoreCase(".gif")) {
00649 isBinary = true;
00650 useCached = false;
00651 contentType = "image/gif";
00652 }
00653 else if (rawDataType.equalsIgnoreCase(".jpg")) {
00654 isBinary = true;
00655 useCached = false;
00656 contentType = "image/jpg";
00657 }
00658 else if (rawDataType.equalsIgnoreCase(".bmp")) {
00659 isBinary = true;
00660 useCached = false;
00661 contentType = "image/bmp";
00662 contentType = "image/x-bmp";
00663 }
00664 else {
00665 isBinary = false;
00666 useCached = false;
00667 contentType = "text/html";
00668 }
00669
00670 JString htmlText;
00671
00672 if (rawDataLength <= 0) {
00673 htmlText = "<HTML>\n";
00674 htmlText += "<HEAD>\n<TITLE>";
00675 htmlText += "No Data Available";
00676 htmlText += "</TITLE>\n</HEAD>\n<BODY>\n";
00677 htmlText += "<H1>Sorry, no data available</H1>";
00678 htmlText += "</BODY>\n";
00679 htmlText += "</HTML>\n";
00680 data = htmlText.getCharCopy(datalength);
00681 result = "HTTP/1.1 404 Object Not Found";
00682 }
00683 else {
00684 data = new char[rawDataLength];
00685 memcpy(data, rawData, rawDataLength);
00686 datalength = rawDataLength;
00687 }
00688
00689 JString text = result;
00690 text.addCRLF();
00691 text += JString("Date: ") + t.toHTTP();
00692 text.addCRLF();
00693 text += "Server: CoreLibrary/0.9";
00694 text.addCRLF();
00695 text += JString("Last-Modified: ") + t.toHTTP();
00696 text.addCRLF();
00697
00698 if (!useCached) {
00699 text += "Cache-Control: no-store, no-cache, must-revalidate, max-age=-1, post-check=0, pre-check=0";
00700 text.addCRLF();
00701 text += "Pragma: no-cache";
00702 text.addCRLF();
00703 text += "Expires: -1";
00704 text.addCRLF();
00705 }
00706
00707
00708
00709
00710
00711 text += JString("Content-Length: ") + JString(datalength);
00712 text.addCRLF();
00713 if (keep_alive)
00714 text += "Connection: Keep-Alive";
00715 else
00716 text += "Connection: close";
00717 text.addCRLF();
00718 text += "Content-Type: ";
00719 text += contentType;
00720 text.addCRLF();
00721
00722 text.addCRLF();
00723
00724 int headerlength;
00725 char* headerdata = text.getCharCopy(headerlength);
00726
00727 char* totaldata = new char[headerlength + datalength];
00728 memcpy(totaldata, headerdata, headerlength);
00729 memcpy(totaldata+headerlength, data, datalength);
00730
00731 delete [] data;
00732 delete [] headerdata;
00733
00734 data = totaldata;
00735 datalength = headerlength + datalength;
00736
00737 if (DEBUGLEVEL(STATUS)) {
00738 printf("HTML Custom Data generated %d bytes of data\n", datalength);
00739 }
00740 return true;
00741
00742 }
00743
00744 HTMLPage* HTMLPage::createErrorPage(const JString& title, const JString& msg) {
00745 HTMLPage* page = new HTMLPage();
00746 page->everything = page->printErrorHTML(title, msg);
00747 return page;
00748 }
00749
00750 HTMLPage* HTMLPage::createSimplePage(const JString& title, const JString& msg) {
00751 HTMLPage* page = new HTMLPage();
00752 page->everything = page->printSimpleHTML(title, msg);
00753 return page;
00754 }
00755
00756 JString HTMLPage::printErrorHTML(const JString& title, const JString& msg) {
00757
00758 JTime t;
00759
00760 JString titl = "Syntax Error";
00761 JString m = "Syntax Error";
00762
00763 if (title.length() > 0)
00764 titl = title;
00765 if (msg.length() > 0)
00766 m = msg;
00767
00768 return JString::format("<HTML>\n<HEAD>\n<TITLE>%s</TITLE>\n</HEAD>\n<BODY>\n%s<br><H2>%s</H2></BODY>\n</HTML>\n",
00769 (char*) titl, (char*) t.print(), (char*) m);
00770 }
00771
00772 JString HTMLPage::printSimpleHTML(const JString& title, const JString& msg) {
00773
00774 JTime t;
00775
00776 JString titl = "";
00777 JString m = "";
00778
00779 if (title.length() > 0)
00780 titl = title;
00781 if (msg.length() > 0)
00782 m = msg;
00783
00784 return JString::format("<HTML>\n<HEAD>\n<TITLE>%s</TITLE>\n</HEAD>\n<BODY>\n%s<br><br>Generated: %s</BODY>\n</HTML>\n",
00785 (char*) titl, (char*) m, (char*) t.print());
00786 }
00787
00788 bool HTMLPage::readFile(const JString& filename, bool isBinary) {
00789
00790 if (data != NULL)
00791 delete [] data;
00792
00793 JString fullname;
00794
00795 if (rootdir.length() > 0) {
00796 if ((!rootdir.endsWith("/")) && (!filename.startsWith("/")))
00797 fullname = rootdir + "/" + filename;
00798 else
00799 fullname = rootdir + filename;
00800 }
00801 else {
00802 fullname = filename;
00803 }
00804
00805 if (isBinary) {
00806 data = JFile::readAFileBinary(fullname, datalength);
00807 }
00808 else {
00809 data = JFile::readAFileASCII(fullname, datalength);
00810 }
00811
00812 return (datalength > 0);
00813 }
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835 HTTPRequest::HTTPRequest() {
00836 complete = false;
00837 ifModifiedSince.setInvalid();
00838 keep_alive = true;
00839 postBytesStillRemaining = -1;
00840 }
00841
00842 HTTPRequest::HTTPRequest(const JString& xml) {
00843 complete = false;
00844 ifModifiedSince.setInvalid();
00845 keep_alive = true;
00846 postBytesStillRemaining = -1;
00847 fromXML(xml);
00848 }
00849
00850 HTTPRequest::HTTPRequest(XMLNode* node) {
00851 complete = false;
00852 ifModifiedSince.setInvalid();
00853 keep_alive = true;
00854 postBytesStillRemaining = -1;
00855 fromXML(node);
00856 }
00857
00858 HTTPRequest::~HTTPRequest() {
00859 paramdict.removeAll();
00860 entries.removeAll();
00861 }
00862
00863 Object* HTTPRequest::clone() const {
00864 HTTPRequest* rq = new HTTPRequest();
00865 *rq = *this;
00866 rq->complete = false;
00867 rq->keep_alive = true;
00868 rq->postBytesStillRemaining = -1;
00869 return (Object*) rq;
00870 }
00871
00872 bool HTTPRequest::fromXML(const JString& xml) {
00873
00874 if (xml.length() == 0)
00875 return false;
00876
00877 XMLParser* xmlParser = new XMLParser();
00878 xmlParser->parse(xml);
00879 bool ret = fromXML(xmlParser->getRootNode());
00880 delete(xmlParser);
00881 return ret;
00882 }
00883
00884 bool HTTPRequest::fromXML(XMLNode* node) {
00885
00886 if ( (node == NULL) || (!node->getTag().equalsIgnoreCase("httprequest")) )
00887 return false;
00888
00889 XMLNode* xmlNode = node->getChildNode("entries");
00890 if (xmlNode != NULL) {
00891 xmlNode = xmlNode->getChildNode("dictionary");
00892 if (xmlNode != NULL)
00893 entries = Dictionary(xmlNode);
00894 }
00895
00896 xmlNode = node->getChildNode("parameters");
00897 if (xmlNode != NULL) {
00898 xmlNode = xmlNode->getChildNode("dictionary");
00899 if (xmlNode != NULL)
00900 paramdict = Dictionary(xmlNode);
00901 }
00902
00903 complete = true;
00904 return true;
00905 }
00906
00907 JString HTTPRequest::toXML() {
00908
00909 JString xml = JString("<entries>\n") + entries.toXML().indentXML() + JString("</entries>\n");
00910 xml += JString("<parameters>\n") + paramdict.toXML().indentXML() + JString("</parameters>\n");
00911 return JString("<httprequest>\n") + xml.indentXML() + JString("</httprequest>\n");
00912 }
00913
00914 bool HTTPRequest::addEntry(const JString& str) {
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928 if (str.length() == 0) {
00929 JString com = entries.get("Command");
00930
00931 if (com.length() == 0) {
00932 printf("HTTP:addEntry '' with no command...\n%s\n\n", (char*) entries.printListLine("\n"));
00933 return true;
00934
00935 }
00936 else if (com.equals("POST")) {
00937 if (entries.contains("Content-Length"))
00938 postBytesStillRemaining = entries.get("Content-Length").toInt();
00939 else if (entries.contains("Content-length"))
00940 postBytesStillRemaining = entries.get("Content-length").toInt();
00941 else
00942 postBytesStillRemaining = 0;
00943 }
00944
00945 if (DEBUGLEVEL(DETAILEDSTATUS)) {
00946 printf("HTTP:allDone '%s'\n", (char*) entries.printListLine(","));
00947 }
00948 complete = true;
00949 return true;
00950 }
00951
00952 Collection oc = str.splitOnWhiteSpaces();
00953
00954 JString cmd = oc.get(0);
00955 oc.removeFirst();
00956 if (cmd.endsWith(":"))
00957 cmd = cmd.substring(0, cmd.length()-1);
00958
00959 JString value = oc.printListLine(" ");
00960
00961 if (cmd.equalsIgnoreCase("if-modified-since"))
00962 ifModifiedSince = JTime(value);
00963
00964
00965
00966 if (entries.contains(cmd))
00967 entries.put(cmd, JString::format("%s,%s", (char*) entries.get(cmd), (char*) value));
00968 else
00969 entries.put(cmd, value);
00970
00971 if ((cmd.equals("GET")) || (cmd.equals("PUT")) || (cmd.equals("POST"))) {
00972 entries.put("Command", cmd);
00973
00974 Collection oc2 = oc.get(0).split("?");
00975 entries.put("request", oc2.get(0).fromHTML());
00976 JString post = oc2.get(1);
00977 if (post.length() > 0)
00978 createParameterDictionary(post);
00979 oc2 = oc.get(1).split("/");
00980 entries.put("protocol", oc2.get(0));
00981 entries.put("protocolversion", oc2.get(1));
00982 }
00983 else if (cmd.equalsIgnoreCase("Connection")) {
00984 if (oc.get(0).equalsIgnoreCase("close"))
00985 keep_alive = false;
00986 }
00987
00988
00989
00990
00991 return true;
00992 }
00993
00994 bool HTTPRequest::addEntry(const JString& key, const JString& val) {
00995 entries.put(key, val);
00996 return true;
00997 }
00998
00999
01000 bool HTTPRequest::isComplete(JSocket* socket) {
01001
01002 if (postBytesStillRemaining > 0) {
01003 if (socket == NULL)
01004 return false;
01005 JString post = socket->readString(postBytesStillRemaining, 1000);
01006 entries.put("POST Content", post);
01007 postBytesStillRemaining -= post.length();
01008 if (postBytesStillRemaining > 0)
01009 return false;
01010 else {
01011 createParameterDictionary(post);
01012 return true;
01013 }
01014 }
01015 else
01016 return complete;
01017 }
01018
01019 bool HTTPRequest::isComplete(NetworkConnection* con) {
01020
01021 if (postBytesStillRemaining > 0) {
01022 if (con == NULL)
01023 return false;
01024 JString post = con->readString(postBytesStillRemaining, 1000);
01025 entries.put("POST Content", post);
01026 postBytesStillRemaining -= post.length();
01027 if (postBytesStillRemaining > 0)
01028 return false;
01029 else {
01030 createParameterDictionary(post);
01031 return true;
01032 }
01033 }
01034 else
01035 return complete;
01036 }
01037
01038
01039 bool HTTPRequest::createParameterDictionary(const JString& post) {
01040
01041 Collection oc = post.split("&");
01042 Collection oc2;
01043
01044 for (int n=0; n<oc.getCount(); n++) {
01045 oc2 = oc.get(n).split("=");
01046 paramdict.put(oc2.get(0).fromHTML(), oc2.get(1).fromHTML());
01047 }
01048 return true;
01049 }
01050
01051 JString HTTPRequest::getParameter(const JString& key) {
01052 return paramdict.get(key).fromHTML();
01053 }
01054
01055 bool HTTPRequest::hasParameter(const JString& key) {
01056 return paramdict.contains(key);
01057 }
01058
01059 JString HTTPRequest::getEntry(const JString& str) {
01060 return entries.get(str);
01061 }
01062
01063 JString HTTPRequest::getCommand() {
01064 return entries.get("Command");
01065 }
01066
01067 JString HTTPRequest::getRequestPage() {
01068 return entries.get("request");
01069 }
01070
01071 JString HTTPRequest::getProtocol() {
01072 return entries.get("protocol");
01073 }
01074
01075 JString HTTPRequest::getProtocolVersion() {
01076 return entries.get("protocolversion");
01077 }
01078
01079 bool HTTPRequest::shouldKeepAlive() {
01080 return keep_alive;
01081 }
01082
01083
01084
01085
01086 }