From: <at...@us...> - 2007-08-23 18:35:46
|
Revision: 488 http://cadcdev.svn.sourceforge.net/cadcdev/?rev=488&view=rev Author: atani Date: 2007-08-23 11:35:40 -0700 (Thu, 23 Aug 2007) Log Message: ----------- update win32 project(s) to include Tiki::Net::Http code added Buffer.read(Tiki::File), Buffer.write(Tiki::File), Buffer(Tiki::File, ...) fixed chunked data size reading added Request.removeHeaderParam(std::string param) Added File.getFileName() Modified Paths: -------------- tiki/examples/net/httpclient/httpclient.vcproj tiki/include/Tiki/file.h tiki/include/Tiki/net/buffer.h tiki/include/Tiki/net/http/request.h tiki/src/base/file.cpp tiki/src/net/http/request.cpp tiki/src/net/http/response.cpp tiki/src/net/http/useragent.cpp tiki/src/net/tcpsocket.cpp tiki/win32/tiki_vc80.sln tiki/win32/tiki_vs80.vcproj Modified: tiki/examples/net/httpclient/httpclient.vcproj =================================================================== --- tiki/examples/net/httpclient/httpclient.vcproj 2007-08-23 00:01:53 UTC (rev 487) +++ tiki/examples/net/httpclient/httpclient.vcproj 2007-08-23 18:35:40 UTC (rev 488) @@ -191,7 +191,7 @@ UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}" > <File - RelativePath=".\src\ChatClient.cpp" + RelativePath=".\src\HttpClient.cpp" > </File> <File Modified: tiki/include/Tiki/file.h =================================================================== --- tiki/include/Tiki/file.h 2007-08-23 00:01:53 UTC (rev 487) +++ tiki/include/Tiki/file.h 2007-08-23 18:35:40 UTC (rev 488) @@ -48,6 +48,8 @@ // Write cnt 32-bit little-endian words to the file. bool writele32( uint32 * in, int cnt ); + // Returns the filename associated with this File object. + string getFileName() const; // Seek in the file. int seek( int where, int whence ); @@ -79,6 +81,7 @@ }; RefPtr<FileFD> m_fd; + string m_fileName; }; } Modified: tiki/include/Tiki/net/buffer.h =================================================================== --- tiki/include/Tiki/net/buffer.h 2007-08-23 00:01:53 UTC (rev 487) +++ tiki/include/Tiki/net/buffer.h 2007-08-23 18:35:40 UTC (rev 488) @@ -9,6 +9,7 @@ #define __TIKI_NET_BUFFER_H #include "Tiki/object.h" +#include "Tiki/File.h" #include <fstream> @@ -16,6 +17,8 @@ namespace Net { +using Tiki::File; + class Buffer : public Object { public: @@ -34,7 +37,8 @@ memcpy(m_data, data, len); } - Buffer(std::string filename, std::string contentType, std::string fieldName = "") : m_fileName(filename), m_contentType(contentType), m_fieldName(fieldName) { + Buffer(std::string filename, std::string contentType, std::string fieldName = "") : + m_fileName(filename), m_contentType(contentType), m_fieldName(fieldName) { std::ifstream stream(filename.c_str(), std::ios::in | std::ios::binary); stream.seekg(0, std::ios::end); m_dataLen = m_usedDataLen = stream.tellg(); @@ -44,6 +48,24 @@ stream.close(); } + Buffer(Tiki::File file, std::string contentType, std::string fieldName = "") : + m_contentType(contentType), m_fieldName(fieldName) { + read(file); + } + + void read(Tiki::File file) { + assert(file.isValid()); + m_fileName = file.getFileName(); + m_dataLen = m_usedDataLen = file.total(); + m_data = new uint8[m_dataLen]; + file.read(m_data, m_dataLen); + } + + void write(Tiki::File file) { + assert(file.isValid()); + file.write(m_data, m_usedDataLen); + } + void append(RefPtr<Buffer> buf) { uint8 * newbuf = new uint8[ m_dataLen + buf->getDataLen() ]; memcpy(newbuf, m_data, m_usedDataLen); @@ -60,6 +82,7 @@ } uint8 *getData() const { + 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 00:01:53 UTC (rev 487) +++ tiki/include/Tiki/net/http/request.h 2007-08-23 18:35:40 UTC (rev 488) @@ -30,6 +30,7 @@ m_url = url; } + void removeHeaderParam(std::string param); void setHeaderParam(std::string param, std::string value); std::string getHeaderParam(std::string param) const; std::list<std::string> getHeaderParamNames() const; Modified: tiki/src/base/file.cpp =================================================================== --- tiki/src/base/file.cpp 2007-08-23 00:01:53 UTC (rev 487) +++ tiki/src/base/file.cpp 2007-08-23 18:35:40 UTC (rev 488) @@ -26,6 +26,7 @@ File::~File() {} bool File::open( const string & fn, const char * access ) { + m_fileName = fn; FILE * f = fopen( fn.c_str(), access ); if ( !f ) return false; @@ -132,6 +133,10 @@ #endif } +string File::getFileName() const { + return m_fileName; +} + int File::seek( int where, int whence ) { if ( !isValid() ) return -1; Modified: tiki/src/net/http/request.cpp =================================================================== --- tiki/src/net/http/request.cpp 2007-08-23 00:01:53 UTC (rev 487) +++ tiki/src/net/http/request.cpp 2007-08-23 18:35:40 UTC (rev 488) @@ -6,6 +6,7 @@ Copyright (C)2007 Atani Software */ +#include "pch.h" #include "Tiki/tiki.h" #include "Tiki/net.h" #include "Tiki/net/http/request.h" @@ -24,11 +25,17 @@ } +void Request::removeHeaderParam(std::string param) { + if(m_params.find(param) != m_params.end()) { + m_params.erase(m_params.find(param)); + } +} + void Request::setHeaderParam(std::string param, std::string value) { -// if(m_params.find(param) != m_params.end()) { -// m_params.erase(m_params.find(param)); -// } - + if(!param.compare("User-Agent")) { + Tiki::Debug::printf("Setting User-Agent via Request::setHeaderParam() is not allowed. Use HttpUserAgent::setUserAgentName() instead.\n"); + return; + } m_params.insert(std::make_pair(param, value)); } Modified: tiki/src/net/http/response.cpp =================================================================== --- tiki/src/net/http/response.cpp 2007-08-23 00:01:53 UTC (rev 487) +++ tiki/src/net/http/response.cpp 2007-08-23 18:35:40 UTC (rev 488) @@ -6,6 +6,7 @@ Copyright (C)2007 Atani Software */ +#include "pch.h" #include "Tiki/tiki.h" #include "Tiki/net.h" #include "Tiki/net/http/request.h" Modified: tiki/src/net/http/useragent.cpp =================================================================== --- tiki/src/net/http/useragent.cpp 2007-08-23 00:01:53 UTC (rev 487) +++ tiki/src/net/http/useragent.cpp 2007-08-23 18:35:40 UTC (rev 488) @@ -6,6 +6,7 @@ Copyright (C)2007 Atani Software */ +#include "pch.h" #include "Tiki/tiki.h" #include "Tiki/net.h" #include "Tiki/net/http/useragent.h" @@ -43,7 +44,17 @@ } HttpUserAgent::HttpUserAgent() { - setUserAgentName("Tiki/1.0"); +#if TIKI_PLAT == TIKI_WIN32 + setUserAgentName("Tiki/1.0 (Windows)"); +#elif TIKI_PLAT == TIKI_NDS + setUserAgentName("Tiki/1.0 (Nintendo DS)"); +#elif TIKI_PLAT == TIKI_SDL + setUserAgentName("Tiki/1.0 (SDL)"); +#elif TIKI_PLAT == TIKI_OSX + setUserAgentName("Tiki/1.0 (Mac OS X)"); +#else + setUserAgentName("Tiki/1.0 (Unknown)"); +#endif setProxyHost(""); setProxyPort(8080); } @@ -79,7 +90,7 @@ } RefPtr<Buffer> buf = new Buffer(2048); - buf->setData((uint8 *)requestText.c_str(), requestText.size()); + buf->setData((uint8 *)requestText.c_str(), requestText.length()); socket->send(buf); readResponse(response, socket); @@ -118,7 +129,7 @@ } RefPtr<Buffer> buf = new Buffer(2048); - buf->setData((uint8 *)requestText.c_str(), requestText.size()); + buf->setData((uint8 *)requestText.c_str(), requestText.length()); socket->send(buf); std::list<std::string> content = req->getContentPartNames(); @@ -130,7 +141,7 @@ std::stringstream temp; temp << req->getBoundaryMarker() << "\r\n"; temp << "Content-Disposition: form-data: name=\""; - if(buf->getFieldName().size() > 0) { + if(!buf->getFieldName().empty()) { temp << buf->getFieldName(); } else { @@ -139,7 +150,7 @@ 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.size()); + headerBuf->setData((uint8 *)headerText.c_str(), headerText.length()); socket->send(headerBuf); socket->send(buf); } @@ -147,7 +158,7 @@ footerText.append(req->getBoundaryMarker()); footerText.append("--\r\n"); RefPtr<Buffer> footerBuf = new Buffer(2048); - footerBuf->setData((uint8 *)footerText.c_str(), footerText.size()); + footerBuf->setData((uint8 *)footerText.c_str(), footerText.length()); socket->send(footerBuf); } else if(content.size() == 1) { @@ -214,9 +225,13 @@ req << *param << ": " << request->getHeaderParam(*param) << "\r\n"; } + if(!m_userAgentName.empty()) { + req << "User-Agent: " << m_userAgentName << "\r\n"; + } + std::list<std::string> content = request->getContentPartNames(); if(!mode.compare("POST") && content.size() > 1) { - uint64 totalSize = request->getBoundaryMarker().size() + 6; // add 6 to account for "--<boundary>--\r\n" + uint64 totalSize = request->getBoundaryMarker().length() + 6; // add 6 to account for "--<boundary>--\r\n" for(std::list<std::string>::iterator iter = content.begin(); iter != content.end(); ++iter) { @@ -224,7 +239,7 @@ totalSize += buf->getUsedDataLen(); std::stringstream temp; temp << "Content-Disposition: form-data: name=\""; - if(buf->getFieldName().size() > 0) { + if(!buf->getFieldName().empty()) { temp << buf->getFieldName(); } else { @@ -233,7 +248,7 @@ temp << "\"; filename=\"" << buf->getFileNameShort() << "\"\r\nContent-Type: " << buf->getContentType() << "\r\n"; // add section header size - totalSize += temp.str().size(); + totalSize += temp.str().length(); } req << "Content-Length: " << totalSize << "\r\n"; @@ -247,7 +262,6 @@ } req << "\r\n"; - Tiki::Debug::printf("Request Text: %s\n", req.str().c_str()); requestText = req.str(); } @@ -255,8 +269,8 @@ std::string status = ""; READ_ONE_LINE(status, socket) - Tiki::Debug::printf("Status: %s\n", status.c_str()); - for(int i = 0; i < status.size(); i++) { + //Tiki::Debug::printf("Status: %s\n", status.c_str()); + for(int i = 0; i < status.length(); i++) { if(status.at(i) == ' ') { response->setResultCode(atoi(status.c_str()+i + 1)); break; @@ -270,6 +284,7 @@ // done with headers break; } + //Tiki::Debug::printf("HEADER_LINE: %s\n", line.c_str()); if(line.find(":") != std::string::npos) { std::string field = line.substr(0, line.find(":")); std::string value = line.substr(line.find(":") + 1); @@ -299,13 +314,17 @@ sizeDecoded = 0; std::string size = ""; READ_ONE_LINE(size, socket); - if(size.size() == 0) { + if(size.empty()) { sizeDecoded = 1; continue; } + while(size.find(" ") != std::string::npos) { + size.erase(size.find(" "), 1); + } + int base = 1; - for(int i = 0; i < size.size(); i++ ) { + for(int i = 0; i < size.length(); i++ ) { char ch = size.at(i); if(ch >= 'A' && ch <= 'F') { ch -= 'A'; @@ -321,7 +340,7 @@ else { continue; } - sizeDecoded += (ch * static_cast<int>(pow(16.0f, size.size() - i - 1))); + sizeDecoded += (ch * static_cast<int>(pow(16.0f, static_cast<int>(size.length() - i - 1)))); } //Tiki::Debug::printf("chunk size: %d [%s]\n", sizeDecoded, size.c_str()); if(sizeDecoded > 0) { @@ -343,12 +362,12 @@ fullBuf->append(chunkBuf); } } while(sizeDecoded > 0); - Tiki::Debug::printf("total size: %d %x\n", totalSize, totalSize); + //Tiki::Debug::printf("total size: %d %x\n", totalSize, totalSize); } else if(response->getHeaderParam("Content-Length").compare("")) { Tiki::Debug::printf("Encoding is inline\n"); long sizeDecoded = atoi(response->getHeaderParam("Content-Length").c_str()); - Tiki::Debug::printf("decodedSize: %d\n", sizeDecoded); + //Tiki::Debug::printf("decodedSize: %d\n", sizeDecoded); RefPtr<Buffer> chunkBuf = new Buffer(sizeDecoded); socket->recv(chunkBuf); Modified: tiki/src/net/tcpsocket.cpp =================================================================== --- tiki/src/net/tcpsocket.cpp 2007-08-23 00:01:53 UTC (rev 487) +++ tiki/src/net/tcpsocket.cpp 2007-08-23 18:35:40 UTC (rev 488) @@ -105,9 +105,9 @@ void TCPSocket::close() { m_open = false; Tiki::Debug::printf("closing socket\n"); - if(m_socket != INVALID_SOCKET) { + if(m_socket != INVALID_SOCKET) { #if TIKI_PLAT == TIKI_WIN32 - shutdown(m_socket, SHUT_RDWR); + ::shutdown(m_socket, SD_BOTH); closesocket(m_socket); #else ::shutdown(m_socket, SHUT_RDWR); Modified: tiki/win32/tiki_vc80.sln =================================================================== --- tiki/win32/tiki_vc80.sln 2007-08-23 00:01:53 UTC (rev 487) +++ tiki/win32/tiki_vc80.sln 2007-08-23 18:35:40 UTC (rev 488) @@ -28,6 +28,11 @@ {F2816CAC-B560-4ED9-8A73-9635F832943C} = {F2816CAC-B560-4ED9-8A73-9635F832943C} EndProjectSection EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "HttpClient", "..\examples\net\httpclient\httpclient.vcproj", "{7B823C96-861C-4578-95FF-1087A45AF1AA}" + ProjectSection(ProjectDependencies) = postProject + {F2816CAC-B560-4ED9-8A73-9635F832943C} = {F2816CAC-B560-4ED9-8A73-9635F832943C} + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -58,6 +63,10 @@ {7B823C96-860C-4578-95EE-1087A45AF1AA}.Debug|Win32.Build.0 = Debug|Win32 {7B823C96-860C-4578-95EE-1087A45AF1AA}.Release|Win32.ActiveCfg = Release|Win32 {7B823C96-860C-4578-95EE-1087A45AF1AA}.Release|Win32.Build.0 = Release|Win32 + {7B823C96-861C-4578-95FF-1087A45AF1AA}.Debug|Win32.ActiveCfg = Debug|Win32 + {7B823C96-861C-4578-95FF-1087A45AF1AA}.Debug|Win32.Build.0 = Debug|Win32 + {7B823C96-861C-4578-95FF-1087A45AF1AA}.Release|Win32.ActiveCfg = Release|Win32 + {7B823C96-861C-4578-95FF-1087A45AF1AA}.Release|Win32.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE Modified: tiki/win32/tiki_vs80.vcproj =================================================================== --- tiki/win32/tiki_vs80.vcproj 2007-08-23 00:01:53 UTC (rev 487) +++ tiki/win32/tiki_vs80.vcproj 2007-08-23 18:35:40 UTC (rev 488) @@ -415,6 +415,22 @@ RelativePath="..\src\net\tcpsocket.cpp" > </File> + <Filter + Name="http" + > + <File + RelativePath="..\src\net\http\request.cpp" + > + </File> + <File + RelativePath="..\src\net\http\response.cpp" + > + </File> + <File + RelativePath="..\src\net\http\useragent.cpp" + > + </File> + </Filter> </Filter> </Filter> <Filter @@ -675,6 +691,26 @@ RelativePath="..\include\Tiki\net\udpsocket.h" > </File> + <Filter + Name="http" + > + <File + RelativePath="..\include\Tiki\net\http\cookie.h" + > + </File> + <File + RelativePath="..\include\Tiki\net\http\request.h" + > + </File> + <File + RelativePath="..\include\Tiki\net\http\response.h" + > + </File> + <File + RelativePath="..\include\Tiki\net\http\useragent.h" + > + </File> + </Filter> </Filter> </Filter> </Filter> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |