[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Myserver-commit] [2742] Cgi::send method refactoring
From: |
Giuseppe Scrivano |
Subject: |
[Myserver-commit] [2742] Cgi::send method refactoring |
Date: |
Thu, 07 Aug 2008 20:42:02 +0000 |
Revision: 2742
http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2742
Author: gscrivano
Date: 2008-08-07 20:42:01 +0000 (Thu, 07 Aug 2008)
Log Message:
-----------
Cgi::send method refactoring
Modified Paths:
--------------
trunk/myserver/src/cgi.cpp
trunk/myserver/src/xml_parser.cpp
Modified: trunk/myserver/src/cgi.cpp
===================================================================
--- trunk/myserver/src/cgi.cpp 2008-08-06 22:11:36 UTC (rev 2741)
+++ trunk/myserver/src/cgi.cpp 2008-08-07 20:42:01 UTC (rev 2742)
@@ -31,6 +31,7 @@
#include "../include/filters_chain.h"
#include "../include/env.h"
#include "../include/pipe.h"
+#include "../include/http_data_handler.h"
#include <string>
#include <sstream>
@@ -444,25 +445,22 @@
if(keepalive)
td->response.connection.assign("keep-alive");
+ /* Send the header. */
+ if(headerSize)
+ HttpHeaders::buildHTTPResponseHeaderStruct(td->buffer2->getBuffer(),
+ &td->response,
+ &(td->nBytesToRead));
/*
*If we have not to append the output send data
*directly to the client.
*/
if(!td->appendOutputs)
{
- string* location = 0;
+ string* location = td->response.getValue("Location", 0);
- /* Send the header. */
- if(headerSize)
- HttpHeaders::buildHTTPResponseHeaderStruct(td->buffer2->getBuffer(),
- &td->response,
- &(td->nBytesToRead));
-
- location = td->response.getValue("Location", 0);
-
/*
- *If it is present Location: xxx in the header
- *send a redirect to xxx.
+ *If it is present Location: foo in the header
+ *send a redirect to `foo'.
*/
if(location && location->length())
{
@@ -476,79 +474,58 @@
HttpHeaders::buildHTTPResponseHeader(td->buffer->getBuffer(),
&td->response);
-
-
td->buffer->setLength((u_int)strlen(td->buffer->getBuffer()));
-
+
if(chain.write(td->buffer->getBuffer(),
static_cast<int>(td->buffer->getLength()), &nbw2))
{
stdInFile.closeFile();
stdOutFile.close();
chain.clearAllFilters();
- /* Remove the connection on sockets error. */
+ /* Remove the connection on sockets error. */
cgiProc.terminateProcess();
return 0;
}
-
- if(onlyHeader)
- {
- stdOutFile.close();
- stdInFile.closeFile();
- chain.clearAllFilters();
- cgiProc.terminateProcess();
- return 1;
- }
}
}
- /* Flush the buffer. Data from the header parsing can be present. */
+ if(!nph && onlyHeader)
+ {
+ stdOutFile.close();
+ stdInFile.closeFile();
+ chain.clearAllFilters();
+ cgiProc.terminateProcess();
+ return 1;
+ }
+
if(headerOffset - headerSize)
{
- if(useChunks)
+ /* Flush the buffer. Data from the header parsing can be present. */
+ if(HttpDataHandler::appendDataToHTTPChannel(td,
+ td->buffer2->getBuffer() +
headerSize,
+ headerOffset - headerSize,
+ &(td->outputData),
+ &chain,
+ td->appendOutputs,
+ useChunks))
{
- ostringstream tmp;
- tmp << hex << (headerOffset-headerSize) << "\r\n";
- td->response.contentLength.assign(tmp.str());
- if(chain.write(tmp.str().c_str(), tmp.str().length(), &nbw2))
- {
- stdOutFile.close();
- stdInFile.closeFile();
- chain.clearAllFilters();
- /* Remove the connection on sockets error. */
- cgiProc.terminateProcess();
- return 0;
- }
- }
-
- if(chain.write((td->buffer2->getBuffer() + headerSize),
- headerOffset-headerSize, &nbw2))
- {
stdOutFile.close();
stdInFile.closeFile();
chain.clearAllFilters();
/* Remove the connection on sockets error. */
cgiProc.terminateProcess();
- return 0;
+ return 0;
}
-
- nbw += nbw2;
-
- if(useChunks && chain.write("\r\n", 2, &nbw2))
- {
- stdOutFile.close();
- stdInFile.closeFile();
- chain.clearAllFilters();
- /* Remove the connection on sockets error. */
- cgiProc.terminateProcess();
- return 0;
- }
+
+ nbw += headerOffset - headerSize;
}
-
+
/* Send the rest of the data until we can read from the pipe. */
for(;;)
{
+ int aliveProcess = 0;
+
/* Process timeout. */
if((int)(getTicks() - procStartTime) > cgiTimeout)
{
@@ -560,7 +537,7 @@
return 0;
}
- int aliveProcess = cgiProc.isProcessAlive();
+ aliveProcess = cgiProc.isProcessAlive();
/* Read data from the process standard output file. */
if(stdOutFile.read(td->buffer2->getBuffer(),
@@ -578,65 +555,25 @@
if(!aliveProcess && !nBytesRead)
break;
- if(nBytesRead)
+ if(nBytesRead &&
+ HttpDataHandler::appendDataToHTTPChannel(td,
+ td->buffer2->getBuffer(),
+ nBytesRead,
+ &(td->outputData),
+ &chain,
+ td->appendOutputs,
+ useChunks))
{
- if(!td->appendOutputs)
- {
- if(useChunks)
- {
- ostringstream tmp;
- tmp << hex << nBytesRead << "\r\n";
- td->response.contentLength.assign(tmp.str());
- if(chain.write(tmp.str().c_str(), tmp.str().length(), &nbw2))
- {
- stdOutFile.close();
- stdInFile.closeFile();
- chain.clearAllFilters();
- /* Remove the connection on sockets error. */
- cgiProc.terminateProcess();
- return 0;
- }
- }
-
- if(chain.write(td->buffer2->getBuffer(), nBytesRead, &nbw2))
- {
- stdOutFile.close();
- stdInFile.closeFile();
- chain.clearAllFilters();
- /* Remove the connection on sockets error. */
- cgiProc.terminateProcess();
- return 0;
- }
-
- nbw += nbw2;
-
- if(useChunks && chain.write("\r\n", 2, &nbw2))
- {
- stdOutFile.close();
- stdInFile.closeFile();
- chain.clearAllFilters();
- /* Remove the connection on sockets error. */
- cgiProc.terminateProcess();
- return 0;
- }
- }
- else/* !td->appendOutputs. */
- {
- if(td->outputData.writeToFile(td->buffer2->getBuffer(),
- nBytesRead, &nbw2))
- {
- stdOutFile.close();
- stdInFile.closeFile();
- chain.clearAllFilters();
- FilesUtility::deleteFile(td->inputDataPath);
- /* Remove the connection on sockets error. */
- cgiProc.terminateProcess();
- return 0;
- }
- nbw += nbw2;
- }
+ stdOutFile.close();
+ stdInFile.closeFile();
+ chain.clearAllFilters();
+ /* Remove the connection on sockets error. */
+ cgiProc.terminateProcess();
+ return 0;
}
- }
+
+ nbw += nBytesRead;
+ }
/* Send the last null chunk if needed. */
@@ -657,11 +594,11 @@
chain.clearAllFilters();
cgiProc.terminateProcess();
-
+
/* Close the stdin and stdout files used by the CGI. */
stdOutFile.close();
stdInFile.closeFile();
-
+
/* Delete the file only if it was created by the CGI module. */
if(!td->inputData.getHandle())
FilesUtility::deleteFile(td->inputDataPath.c_str());
Modified: trunk/myserver/src/xml_parser.cpp
===================================================================
--- trunk/myserver/src/xml_parser.cpp 2008-08-06 22:11:36 UTC (rev 2741)
+++ trunk/myserver/src/xml_parser.cpp 2008-08-07 20:42:01 UTC (rev 2742)
@@ -23,26 +23,6 @@
#include <string.h>
}
-
-#ifdef WIN32
-/*!
- * Libxml2.lib is the dynamic version of the libxml2 library
- * libxml2_a.lib is the static version of the libxml2 library
- *
- * For the static version, please use the following linker options:
- * /NODEFAULTLIB:LIBCMT
- * /NODEFAULTLIB:LIBCMTD
- *
- */
-
-#ifdef LIXML_STATICALLY_LINKED
-#pragma comment (lib,"libxml2_a.lib")
-#else
-#pragma comment (lib,"libxml2.lib")
-#endif
-
-#endif
-
/**
* Internal call back functions for saveMemBuf
* \param context Context
@@ -98,29 +78,29 @@
*/
int XmlParser::open(const char* filename)
{
- cur=0;
-
+ cur = 0;
+
if(!FilesUtility::fileExists(filename))
return -1;
-
+
if(doc!=0)
close();
-
+
doc = xmlParseFile(filename);
-
+
if(doc == 0)
return -1;
-
+
cur = xmlDocGetRootElement(doc);
-
+
if(!cur)
{
close();
return -1;
}
-
+
mtime = FilesUtility::getLastModTime(filename);
-
+
if(mtime == static_cast<time_t>(-1))
{
close();
@@ -147,35 +127,34 @@
*/
int XmlParser::openMemBuf(MemBuf & memory)
{
- mtime=0;
- cur=0;
+ mtime = 0;
+ cur = 0;
if(memory.getLength() == 0)
return -1;
- if(doc==0)
+ if(!doc)
{
doc = xmlParseMemory((const char * )memory.getBuffer(),
- memory.getLength());
+ memory.getLength());
}
else
close();
if(!doc)
return -1;
-
+
cur = xmlDocGetRootElement(doc);
-
+
if(!cur)
{
close();
return -1;
}
-
+
return 0;
}
-
/**
* Constructor of the XmlParser class
*/
@@ -187,7 +166,6 @@
lastNode = 0;
}
-
/**
* Destructor of the XmlParser class
* Destroys the XmlParser object
@@ -216,10 +194,10 @@
char *ret = 0;
xmlNodePtr lcur;
cur = xmlDocGetRootElement(doc);
-
+
if(!cur)
return 0;
-
+
lcur = cur->xmlChildrenNode;
buffer.assign("");
@@ -317,7 +295,7 @@
}
}
- lcur=lcur->next;
+ lcur = lcur->next;
}
return 0;
@@ -352,7 +330,7 @@
*/
int XmlParser::save(const char *filename,int *nbytes)
{
- int err=xmlSaveFile(filename,doc);
+ int err = xmlSaveFile(filename,doc);
if(nbytes)
*nbytes = err;
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Myserver-commit] [2742] Cgi::send method refactoring,
Giuseppe Scrivano <=