From: <at...@us...> - 2007-08-24 02:55:11
|
Revision: 491 http://cadcdev.svn.sourceforge.net/cadcdev/?rev=491&view=rev Author: atani Date: 2007-08-23 19:55:09 -0700 (Thu, 23 Aug 2007) Log Message: ----------- more fixes to uploading files via POST. Added Request.setForcedMultiPartUpload() commented many Debug::printf lines Modified Paths: -------------- tiki/include/Tiki/net/buffer.h tiki/include/Tiki/net/http/request.h tiki/src/net/http/request.cpp tiki/src/net/http/useragent.cpp tiki/src/net/tcpsocket.cpp Modified: tiki/include/Tiki/net/buffer.h =================================================================== --- tiki/include/Tiki/net/buffer.h 2007-08-23 23:28:54 UTC (rev 490) +++ tiki/include/Tiki/net/buffer.h 2007-08-24 02:55:09 UTC (rev 491) @@ -8,7 +8,7 @@ #ifndef __TIKI_NET_BUFFER_H #define __TIKI_NET_BUFFER_H -#include "Tiki/object.h" +#include "Tiki/refcnt.h" #include "Tiki/file.h" #include <fstream> @@ -19,13 +19,15 @@ using Tiki::File; -class Buffer : public Object +class Buffer : public RefCnt { public: Buffer(size_t len, std::string contentType = "application/octet-stream") : m_contentType(contentType) { - m_data = new uint8[len]; - memset(m_data, 0, len); + if(len > 0) { + m_data = new uint8[len]; + memset(m_data, 0, len); + } m_dataLen = len; m_usedDataLen = 0; } @@ -64,9 +66,11 @@ file.write(m_data, static_cast<int>(m_usedDataLen)); } - void append(RefPtr<Buffer> buf) { + void append(Buffer *buf) { uint8 * newbuf = new uint8[ m_dataLen + buf->getDataLen() ]; - memcpy(newbuf, m_data, m_usedDataLen); + if(m_data != NULL) { + memcpy(newbuf, m_data, m_usedDataLen); + } memcpy(newbuf + m_usedDataLen, buf->getData(), buf->getUsedDataLen()); delete [] m_data; m_data = newbuf; @@ -75,12 +79,16 @@ } void reset() { - memset(m_data, 0, m_dataLen); + if(m_data != NULL) { + memset(m_data, 0, m_dataLen); + } m_usedDataLen = 0; } uint8 *getData() const { - m_data[m_usedDataLen] = '\0'; + if(m_usedDataLen > 0) { + m_data[m_usedDataLen] = '\0'; + } return m_data; } Modified: tiki/include/Tiki/net/http/request.h =================================================================== --- tiki/include/Tiki/net/http/request.h 2007-08-23 23:28:54 UTC (rev 490) +++ tiki/include/Tiki/net/http/request.h 2007-08-24 02:55:09 UTC (rev 491) @@ -56,6 +56,14 @@ return m_boundaryMarker; } + void setForcedMultiPartUpload(bool mode) { + m_forcedMultiPart = mode; + } + + bool isForcedMultiPartUpload() const { + return m_forcedMultiPart; + } + private: std::string m_url; std::string m_boundaryMarker; @@ -63,6 +71,7 @@ typedef std::map<std::string, RefPtr<Buffer> > StringBufferMap; StringStringMap m_params; StringBufferMap m_parts; + bool m_forcedMultiPart; }; }; // namespace Http Modified: tiki/src/net/http/request.cpp =================================================================== --- tiki/src/net/http/request.cpp 2007-08-23 23:28:54 UTC (rev 490) +++ tiki/src/net/http/request.cpp 2007-08-24 02:55:09 UTC (rev 491) @@ -21,8 +21,8 @@ Request::Request() { setHeaderParam("Connection", "close"); - m_boundaryMarker = "-----------------------09f911019d74e35bd84156c5635688c0"; - + m_boundaryMarker = "--09f911019d74e35bd84156c5635688c0"; + m_forcedMultiPart = false; } void Request::removeHeaderParam(std::string param) { Modified: tiki/src/net/http/useragent.cpp =================================================================== --- tiki/src/net/http/useragent.cpp 2007-08-23 23:28:54 UTC (rev 490) +++ tiki/src/net/http/useragent.cpp 2007-08-24 02:55:09 UTC (rev 491) @@ -24,7 +24,7 @@ #define READ_ONE_LINE(res, socket) \ { \ - RefPtr<Buffer> recvBuf = new Buffer(1); \ + Buffer *recvBuf = new Buffer(1); \ char tbuf[2]; \ res = ""; \ while(socket->isOpen()) { \ @@ -41,6 +41,7 @@ } \ } \ } \ + delete recvBuf; \ } HttpUserAgent::HttpUserAgent() { @@ -73,9 +74,9 @@ std::string requestText; buildRequest(hostname, resource, port, "GET", req, requestText); - Tiki::Debug::printf("Request:\n%s", requestText.c_str()); + //Tiki::Debug::printf("Request:\n%s", requestText.c_str()); - RefPtr<TCPSocket> socket; + TCPSocket *socket; if(m_proxyHost.empty()) { socket = new TCPSocket(new Address(hostname, port)); } @@ -86,24 +87,20 @@ socket->setNonBlocking(false); socket->open(); - if(socket->isOpen()) { - Tiki::Debug::printf("Sending request...\n"); - } - else { + if(!socket->isOpen()) { Tiki::Debug::printf("connect failed\n"); response->setResultCode(504); return response; } - RefPtr<Buffer> buf = new Buffer(2048); - buf->setData((uint8 *)requestText.c_str(), requestText.length()); - socket->send(buf); + Tiki::Debug::printf("Sending request...\n"); + socket->send(new Buffer(requestText.length(), (uint8 *)requestText.c_str())); readResponse(response, socket); socket->close(); - //delete socket; + delete socket; return response; } @@ -121,9 +118,9 @@ std::string requestText; buildRequest(hostname, resource, port, "POST", req, requestText); - Tiki::Debug::printf("Request:\n%s", requestText.c_str()); + //Tiki::Debug::printf("Request:\n%s", requestText.c_str()); - RefPtr<TCPSocket> socket; + TCPSocket *socket; if(m_proxyHost.empty()) { socket = new TCPSocket(new Address(hostname, port)); } @@ -134,61 +131,59 @@ socket->setNonBlocking(false); socket->open(); - if(socket->isOpen()) { - Tiki::Debug::printf("Sending request...\n"); - } - else { + if(!socket->isOpen()) { Tiki::Debug::printf("connect failed\n"); response->setResultCode(504); return response; } - RefPtr<Buffer> buf = new Buffer(2048); - buf->setData((uint8 *)requestText.c_str(), requestText.length()); - socket->send(buf); + Tiki::Debug::printf("Sending request...\n"); + socket->send(new Buffer(requestText.length(), (uint8 *)requestText.c_str())); std::list<std::string> content = req->getContentPartNames(); - if(content.size() > 1) { + if(content.size() > 1 || req->isForcedMultiPartUpload()) { std::string status = ""; READ_ONE_LINE(status, socket) for(std::list<std::string>::iterator iter = content.begin(); iter != content.end(); ++iter) { - RefPtr<Buffer> buf = req->getContentPart(*iter); - std::stringstream temp; - temp << req->getBoundaryMarker() << "\r\n"; - temp << "Content-Disposition: form-data: name=\""; - if(!buf->getFieldName().empty()) { - temp << buf->getFieldName(); + Buffer *buf = req->getContentPart(*iter); + if(buf->getUsedDataLen() > 0) { + std::stringstream temp; + temp << "--" << req->getBoundaryMarker() << "\r\n"; + temp << "Content-Disposition: form-data: name=\""; + if(!buf->getFieldName().empty()) { + temp << buf->getFieldName(); + } + else { + temp << "File"; + } + temp << "\"; filename=\"" << buf->getFileNameShort() << "\"\r\nContent-Type: " << buf->getContentType() << "\r\n"; + std::string headerText = temp.str(); + //Tiki::Debug::printf("CONTENT_HEADER:\n%s", headerText.c_str()); + socket->send(new Buffer(headerText.length(), (uint8 *)headerText.c_str())); + socket->send(buf); } - else { - temp << "File"; - } - temp << "\"; filename=\"" << buf->getFileNameShort() << "\"\r\nContent-Type: " << buf->getContentType() << "\r\n"; - RefPtr<Buffer> headerBuf = new Buffer(2048); - std::string headerText = temp.str(); - headerBuf->setData((uint8 *)headerText.c_str(), headerText.length()); - socket->send(headerBuf); - socket->send(buf); } std::string footerText = "--"; footerText.append(req->getBoundaryMarker()); footerText.append("--\r\n"); - RefPtr<Buffer> footerBuf = new Buffer(2048); - footerBuf->setData((uint8 *)footerText.c_str(), footerText.length()); - socket->send(footerBuf); + //Tiki::Debug::printf("CONTENT_FOOTER:\n%s", footerText.c_str()); + socket->send(new Buffer(footerText.length(), (uint8 *)footerText.c_str())); } else if(content.size() == 1) { - RefPtr<Buffer> buf = req->getContentPart(*content.begin()); - socket->send(buf); + Buffer *buf = req->getContentPart(*content.begin()); + if(buf->getUsedDataLen() > 0) { + socket->send(buf); + } } readResponse(response, socket); socket->close(); - //delete socket; + delete socket; return response; } @@ -247,25 +242,27 @@ } std::list<std::string> content = request->getContentPartNames(); - if(!mode.compare("POST") && content.size() > 1) { - uint64 totalSize = request->getBoundaryMarker().length() + 6; // add 6 to account for "--<boundary>--\r\n" + if(!mode.compare("POST") && (content.size() > 1 || request->isForcedMultiPartUpload())) { + uint64 totalSize = 8; // add 8 to account for "--<boundary>--\r\n" for(std::list<std::string>::iterator iter = content.begin(); iter != content.end(); ++iter) { - RefPtr<Buffer> buf = request->getContentPart(*iter); - totalSize += buf->getUsedDataLen(); - std::stringstream temp; - temp << "Content-Disposition: form-data: name=\""; - if(!buf->getFieldName().empty()) { - temp << buf->getFieldName(); + Buffer *buf = request->getContentPart(*iter); + if(buf->getUsedDataLen() > 0) { + totalSize += buf->getUsedDataLen(); + std::stringstream temp; + temp << "Content-Disposition: form-data: name=\""; + if(!buf->getFieldName().empty()) { + temp << buf->getFieldName(); + } + else { + temp << "File"; + } + temp << "\"; filename=\"" << buf->getFileNameShort() << "\"\r\nContent-Type: " << buf->getContentType() << "\r\n"; + + // add section header size + totalSize += temp.str().length(); } - else { - temp << "File"; - } - temp << "\"; filename=\"" << buf->getFileNameShort() << "\"\r\nContent-Type: " << buf->getContentType() << "\r\n"; - - // add section header size - totalSize += temp.str().length(); } req << "Content-Length: " << totalSize << "\r\n"; @@ -273,7 +270,7 @@ req << "Content-Type: multipart/form-data; boundary=" << request->getBoundaryMarker() << "\r\n"; } else if(!mode.compare("POST") && content.size() == 1) { - RefPtr<Buffer> buf = request->getContentPart(*content.begin()); + Buffer *buf = request->getContentPart(*content.begin()); req << "Content-Type: " << buf->getContentType() << "\r\n"; req << "Content-Length: " << buf->getUsedDataLen() << "\r\n"; } @@ -344,21 +341,23 @@ //Tiki::Debug::printf("chunk size: %d [%s]\n", sizeDecoded, size.c_str()); if(sizeDecoded > 0) { - RefPtr<Buffer> chunkBuf = new Buffer(sizeDecoded); + Buffer *chunkBuf = new Buffer(sizeDecoded); socket->recv(chunkBuf); if(chunkBuf->getUsedDataLen() < sizeDecoded) { //Tiki::Debug::printf("Buffer underflow\n"); size_t needed = sizeDecoded - chunkBuf->getUsedDataLen(); while(needed > 0) { - RefPtr<Buffer> chunkBuf2 = new Buffer(needed); + Buffer *chunkBuf2 = new Buffer(needed); socket->recv(chunkBuf2); chunkBuf->append(chunkBuf2); needed -= chunkBuf2->getUsedDataLen(); + delete chunkBuf2; } } totalSize += chunkBuf->getUsedDataLen(); fullBuf->append(chunkBuf); + delete chunkBuf; } } while(sizeDecoded > 0); //Tiki::Debug::printf("total size: %d %x\n", totalSize, totalSize); @@ -368,22 +367,30 @@ size_t sizeDecoded = atoi(response->getHeaderParam("Content-Length").c_str()); //Tiki::Debug::printf("decodedSize: %d\n", sizeDecoded); - RefPtr<Buffer> chunkBuf = new Buffer(sizeDecoded); + Buffer *chunkBuf = new Buffer(sizeDecoded); socket->recv(chunkBuf); if(chunkBuf->getUsedDataLen() < sizeDecoded) { sizeDecoded -= chunkBuf->getUsedDataLen(); while(sizeDecoded > 0) { - RefPtr<Buffer> chunkBuf2 = new Buffer(sizeDecoded); + Buffer *chunkBuf2 = new Buffer(sizeDecoded); socket->recv(chunkBuf2); chunkBuf->append(chunkBuf2); sizeDecoded -= chunkBuf2->getUsedDataLen(); + delete chunkBuf2; } } fullBuf->append(chunkBuf); + delete chunkBuf; } else { Tiki::Debug::printf("Encoding is unknown\n"); + Tiki::Debug::printf("Dumping headers\nHEADER NAME -> VALUE"); + for(std::list<std::string>::iterator iter = response->getHeaderParamNames().begin(); + iter != response->getHeaderParamNames().end(); + ++iter) { + Tiki::Debug::printf("%s -> %s\n", (*iter).c_str(), response->getHeaderParam(*iter).c_str()); + } } response->addContentPart(fullBuf, DEFAULT_CONTENT_PART); } Modified: tiki/src/net/tcpsocket.cpp =================================================================== --- tiki/src/net/tcpsocket.cpp 2007-08-23 23:28:54 UTC (rev 490) +++ tiki/src/net/tcpsocket.cpp 2007-08-24 02:55:09 UTC (rev 491) @@ -24,10 +24,10 @@ int len; do { - Tiki::Debug::printf("sending %d bytes\n", dataLen); + //Tiki::Debug::printf("sending %d bytes\n", dataLen); len = ::send(m_socket, (const char *)data, (int)dataLen, 0); if(len > 0) { - Tiki::Debug::printf("sent %d bytes\n", len); + //Tiki::Debug::printf("sent %d bytes\n", len); dataLen -= len; data += len; } @@ -71,7 +71,7 @@ int yes = 1; #if TIKI_PLAT != TIKI_NDS - Tiki::Debug::printf("setting TCP_NODELAY\n"); + //Tiki::Debug::printf("setting TCP_NODELAY\n"); setsockopt(m_socket, IPPROTO_TCP, TCP_NODELAY, (char *)&yes, sizeof(yes)); #endif @@ -91,13 +91,13 @@ sock_addr.sin_family = AF_INET; sock_addr.sin_addr.s_addr = getPeerAddress()->getIPAddress(); sock_addr.sin_port = htons(getPeerAddress()->getPort()); - Tiki::Debug::printf("connecting to peer [%s->%s]\n", getPeerAddress()->getHostName().c_str(), getPeerAddress()->getIPAddressString().c_str()); + //Tiki::Debug::printf("connecting to peer [%s->%s]\n", getPeerAddress()->getHostName().c_str(), getPeerAddress()->getIPAddressString().c_str()); if( ::connect( m_socket, (struct sockaddr *)&sock_addr, sizeof(struct sockaddr_in)) == SOCKET_ERROR) { Tiki::Debug::printf("Error opening socket\n"); close(); return; } - Tiki::Debug::printf("Connected..\n"); + //Tiki::Debug::printf("Connected..\n"); } m_open = true; } @@ -118,7 +118,7 @@ void TCPSocket::setNonBlocking(bool blocking) { - Tiki::Debug::printf("%sabling blocking\n", blocking ? "dis" : "en" ); + //Tiki::Debug::printf("%sabling blocking\n", blocking ? "dis" : "en" ); Socket::setNonBlocking(blocking); #if TIKI_PLAT == TIKI_WIN32 unsigned long mode = (!blocking); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |