From: <at...@us...> - 2007-08-21 23:56:18
|
Revision: 482 http://cadcdev.svn.sourceforge.net/cadcdev/?rev=482&view=rev Author: atani Date: 2007-08-21 16:56:11 -0700 (Tue, 21 Aug 2007) Log Message: ----------- Tiki::Net::Http implementation for GET Modified Paths: -------------- tiki/include/Tiki/net/buffer.h tiki/sdl/Makefile Added Paths: ----------- tiki/examples/net/httpclient/ tiki/examples/net/httpclient/Makefile tiki/examples/net/httpclient/httpclient.vcproj tiki/examples/net/httpclient/src/ tiki/examples/net/httpclient/src/HttpClient.cpp tiki/examples/net/httpclient/src/main.cpp tiki/include/Tiki/net/http/ tiki/include/Tiki/net/http/cookie.h tiki/include/Tiki/net/http/request.h tiki/include/Tiki/net/http/response.h tiki/include/Tiki/net/http/useragent.h tiki/src/net/http/ tiki/src/net/http/request.cpp tiki/src/net/http/response.cpp tiki/src/net/http/useragent.cpp Added: tiki/examples/net/httpclient/Makefile =================================================================== --- tiki/examples/net/httpclient/Makefile (rev 0) +++ tiki/examples/net/httpclient/Makefile 2007-08-21 23:56:11 UTC (rev 482) @@ -0,0 +1,13 @@ + +TIKI_DIR=../../../ +CFLAGS=-I$(TIKI_DIR)$(TIKI_PLAT)/include -I$(TIKI_DIR)include +OBJS = $(patsubst %.cpp,%.o,$(wildcard src/*.cpp)) + +all: $(OBJS) + $(CXX) $(LDFLAGS) -L$(TIKI_DIR)$(TIKI_PLAT) -L$(TIKI_DIR)$(TIKI_PLAT)/lib $(OBJS) $(TIKI_BASE_LIBS) -o httpclient + +clean: + -rm -f $(OBJS) httpclient + +DEPSDIR=$(CURDIR) +include $(TIKI_DIR)$(TIKI_PLAT)/Makefile.rules Added: tiki/examples/net/httpclient/httpclient.vcproj =================================================================== --- tiki/examples/net/httpclient/httpclient.vcproj (rev 0) +++ tiki/examples/net/httpclient/httpclient.vcproj 2007-08-21 23:56:11 UTC (rev 482) @@ -0,0 +1,205 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="8.00" + Name="HttpClient" + ProjectGUID="{7B823C96-861C-4578-95FF-1087A45AF1AA}" + Keyword="Win32Proj" + > + <Platforms> + <Platform + Name="Win32" + /> + </Platforms> + <ToolFiles> + </ToolFiles> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory="Debug" + IntermediateDirectory="Debug" + ConfigurationType="1" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + AdditionalIncludeDirectories="$(ProjectDir)\..\..\..\win32\include;$(ProjectDir)\..\..\..\include;"C:\Program Files\OpenAL 1.1 SDK\include"" + PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;" + MinimalRebuild="true" + BasicRuntimeChecks="3" + RuntimeLibrary="1" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="true" + DebugInformationFormat="4" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + AdditionalDependencies="gdi32.lib kernel32.lib user32.lib opengl32.lib glu32.lib comdlg32.lib ws2_32.lib" + LinkIncremental="2" + AdditionalLibraryDirectories="$(ProjectDir)\..\..\..\win32\Debug;"C:\Program Files\OpenAL 1.1 SDK\libs\Win32"" + GenerateDebugInformation="true" + SubSystem="2" + TargetMachine="1" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCWebDeploymentTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="Release|Win32" + OutputDirectory="Release" + IntermediateDirectory="Release" + ConfigurationType="1" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + AdditionalIncludeDirectories="$(ProjectDir)\..\..\..\win32\include;$(ProjectDir)\..\..\..\include;"C:\Program Files\OpenAL 1.1 SDK\include"" + PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;" + RuntimeLibrary="0" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="true" + DebugInformationFormat="3" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + AdditionalDependencies="gdi32.lib kernel32.lib user32.lib opengl32.lib glu32.lib comdlg32.lib ws2_32.lib" + LinkIncremental="1" + AdditionalLibraryDirectories="$(ProjectDir)\..\..\..\win32\Release;C:\Program Files\OpenAL 1.1 SDK\libs\Win32" + IgnoreDefaultLibraryNames="" + GenerateDebugInformation="true" + SubSystem="2" + OptimizeReferences="2" + EnableCOMDATFolding="2" + TargetMachine="1" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCWebDeploymentTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="Header Files" + Filter="h;hpp;hxx;hm;inl;inc;xsd" + UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}" + > + </Filter> + <Filter + Name="Resource Files" + Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx" + UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}" + > + </Filter> + <Filter + Name="Source Files" + Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx" + UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}" + > + <File + RelativePath=".\src\ChatClient.cpp" + > + </File> + <File + RelativePath=".\src\main.cpp" + > + </File> + </Filter> + </Files> + <Globals> + </Globals> +</VisualStudioProject> Added: tiki/examples/net/httpclient/src/HttpClient.cpp =================================================================== --- tiki/examples/net/httpclient/src/HttpClient.cpp (rev 0) +++ tiki/examples/net/httpclient/src/HttpClient.cpp 2007-08-21 23:56:11 UTC (rev 482) @@ -0,0 +1,27 @@ +/* +* HttpClient.cpp +* HttpClient +* +* Copyright (C)2007 Atani Software +* +*/ + +#include <Tiki/tiki.h> +#include <pch.h> + +#if TIKI_PLAT == TIKI_WIN32 +#include <windows.h> + +static char szAppName[] = "HttpClient"; +int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) +#else +extern "C" int tiki_main( int argc, char *argv[] ); +int main( int argc, char *argv[] ) +#endif +{ +#if TIKI_PLAT != TIKI_WIN32 + return tiki_main( argc, argv ); +#else + return Tiki::DoMain( szAppName, hInst, hPrevInstance, lpCmdLine, nCmdShow ); +#endif +} Added: tiki/examples/net/httpclient/src/main.cpp =================================================================== --- tiki/examples/net/httpclient/src/main.cpp (rev 0) +++ tiki/examples/net/httpclient/src/main.cpp 2007-08-21 23:56:11 UTC (rev 482) @@ -0,0 +1,61 @@ +/* +* main.cpp +* Simple Http Client Main method +* +* Copyright (C)2007 Atani Software +* +*/ + +#include <Tiki/tiki.h> +#include <Tiki/refcnt.h> +#include <Tiki/hid.h> +#include <Tiki/tikitime.h> +#include <Tiki/net.h> +#include <Tiki/net/http/useragent.h> +#include <Tiki/net/http/request.h> + +using namespace Tiki; +using namespace Tiki::Debug; +using namespace Tiki::Net; +using namespace Tiki::Net::Http; + +volatile bool g_quitting = false; +void tkCallback( const Hid::Event & evt, void * data ) { + if ( evt.type == Hid::Event::EvtQuit ) { + g_quitting = true; + } +} + +extern "C" int tiki_main( int argc, char **argv) { + Tiki::init(argc, argv); + Tiki::Net::init(); + Hid::callbackReg( tkCallback, NULL ); + + if(argc < 2) { + Tiki::Debug::printf("Need to pass request url on commandline\n"); + return -1; + } + + RefPtr<HttpUserAgent> useragent = new HttpUserAgent(); + useragent->setProxyHost("www-proxy.us.oracle.com"); + useragent->setProxyPort(80); + + RefPtr<Request> request = new Request(); + request->setUrl(argv[1]); + + RefPtr<Response> response = useragent->get(request); + Tiki::Debug::printf("response code: %d\n", response->getResultCode()); + + std::list<std::string> content = response->getContentPartNames(); + for(std::list<std::string>::iterator iter = content.begin(); + iter != content.end(); + ++iter) + { + RefPtr<Buffer> responseBuf = response->getContentPart(*iter); + Tiki::Debug::printf("Content Part: %s [%u bytes]\n", (*iter).c_str(), responseBuf->getUsedDataLen()); + } + + + Tiki::Net::shutdown(); + return 0; +} Modified: tiki/include/Tiki/net/buffer.h =================================================================== --- tiki/include/Tiki/net/buffer.h 2007-08-21 16:14:06 UTC (rev 481) +++ tiki/include/Tiki/net/buffer.h 2007-08-21 23:56:11 UTC (rev 482) @@ -21,14 +21,26 @@ m_data = new uint8[len]; memset(m_data, 0, len); m_dataLen = len; + m_usedDataLen = 0; } Buffer(size_t len, uint8 *data) { m_data = new uint8[len]; memset(m_data, 0, len); m_dataLen = len; + m_usedDataLen = len; memcpy(m_data, data, len); } + void append(RefPtr<Buffer> buf) { + uint8 * newbuf = new uint8[ m_dataLen + buf->getDataLen() ]; + memcpy(newbuf, m_data, m_usedDataLen); + memcpy(newbuf + m_usedDataLen, buf->getData(), buf->getUsedDataLen()); + delete [] m_data; + m_data = newbuf; + m_dataLen += buf->getDataLen(); + m_usedDataLen += buf->getUsedDataLen(); + } + void reset() { memset(m_data, 0, m_dataLen); m_usedDataLen = 0; Added: tiki/include/Tiki/net/http/cookie.h =================================================================== --- tiki/include/Tiki/net/http/cookie.h (rev 0) +++ tiki/include/Tiki/net/http/cookie.h 2007-08-21 23:56:11 UTC (rev 482) @@ -0,0 +1,72 @@ +/* + Tiki + + useragent.h + + Copyright (C)2007 Atani Software +*/ +#ifndef __TIKI_NET_HTTP_COOKIE_H +#define __TIKI_NET_HTTP_COOKIE_H + +#include "Tiki/refcnt.h" + +namespace Tiki { + +namespace Net { + +namespace Http { + +class Cookie : public RefCnt { + + public: + Cookie( std::string name, std::string value, std::string version, + std::string comment = "", long maxage = 0, std::string path = "", + bool secure = false) : + m_name(name), m_value(value), m_version(version), m_comment(comment), + m_maxage(maxage), m_path(path), m_secure(secure) {}; + + std::string getName() const { + return m_name; + } + + std::string getValue() const { + return m_value; + } + + std::string getVersion() const { + return m_version; + } + + std::string getComment() const { + return m_comment; + } + + std::string getPath() const { + return m_path; + } + + long getMaxAge() const { + return m_maxage; + } + + bool isSecure() { + return m_secure; + } + private: + std::string m_name; + std::string m_value; + std::string m_version; + std::string m_comment; + long m_maxage; + std::string m_path; + bool m_secure; +}; + + +}; // namespace Http + +}; // namespace Net + +}; // namespace Tiki + +#endif // __TIKI_NET_HTTP_COOKIE_H Added: tiki/include/Tiki/net/http/request.h =================================================================== --- tiki/include/Tiki/net/http/request.h (rev 0) +++ tiki/include/Tiki/net/http/request.h 2007-08-21 23:56:11 UTC (rev 482) @@ -0,0 +1,55 @@ +/* + Tiki + + useragent.h + + Copyright (C)2007 Atani Software +*/ +#ifndef __TIKI_NET_HTTP_REQUEST_H +#define __TIKI_NET_HTTP_REQUEST_H + +#include "Tiki/refcnt.h" + +namespace Tiki { + +namespace Net { + +namespace Http { + +extern std::string DEFAULT_CONTENT_PART; + +class Request : public RefCnt { + public: + Request(); + + std::string getUrl() const { + return m_url; + } + + void setUrl(string url) { + m_url = url; + } + + void setHeaderParam(std::string param, std::string value); + std::string getHeaderParam(std::string param) const; + std::list<std::string> getHeaderParamNames() const; + + void addContentPart(std::string name, RefPtr<Buffer> input); + std::list<std::string> getContentPartNames() const; + RefPtr<Buffer> getContentPart(std::string name) const; + + private: + std::string m_url; + typedef std::map<std::string, std::string> StringStringMap; + typedef std::map<std::string, RefPtr< Buffer > > StringBufferMap; + StringStringMap m_params; + StringBufferMap m_parts; +}; + +}; // namespace Http + +}; // namespace Net + +}; // namespace Tiki + +#endif // __TIKI_NET_HTTP_REQUEST_H Added: tiki/include/Tiki/net/http/response.h =================================================================== --- tiki/include/Tiki/net/http/response.h (rev 0) +++ tiki/include/Tiki/net/http/response.h 2007-08-21 23:56:11 UTC (rev 482) @@ -0,0 +1,37 @@ +/* + Tiki + + useragent.h + + Copyright (C)2007 Atani Software +*/ +#ifndef __TIKI_NET_HTTP_RESPONSE_H +#define __TIKI_NET_HTTP_RESPONSE_H + +namespace Tiki { + +namespace Net { + +namespace Http { + +class Response : public Request { + public: + Response(); + + int getResultCode() const { + return m_resultCode; + } + void setResultCode(int code) { + m_resultCode = code; + } + private: + int m_resultCode; +}; + +}; // namespace Http + +}; // namespace Net + +}; // namespace Tiki + +#endif // __TIKI_NET_HTTP_RESPONSE_H Added: tiki/include/Tiki/net/http/useragent.h =================================================================== --- tiki/include/Tiki/net/http/useragent.h (rev 0) +++ tiki/include/Tiki/net/http/useragent.h 2007-08-21 23:56:11 UTC (rev 482) @@ -0,0 +1,74 @@ +/* + Tiki + + useragent.h + + Copyright (C)2007 Atani Software +*/ +#ifndef __TIKI_NET_HTTP_USERAGENT_H +#define __TIKI_NET_HTTP_USERAGENT_H + +#include "Tiki/refcnt.h" +#include "Tiki/net/http/cookie.h" +#include "Tiki/net/http/request.h" +#include "Tiki/net/http/response.h" + +namespace Tiki { + +namespace Net { + +namespace Http { + +class HttpUserAgent : public RefCnt { + + public: + HttpUserAgent(); + + void setUserAgentName(std::string name) { + m_userAgentName = name; + } + + std::string getUserAgentName() const { + return m_userAgentName; + } + + void setProxyHost(std::string host) { + m_proxyHost = host; + } + + std::string getProxyHost() const { + return m_proxyHost; + } + + void setProxyPort(int port) { + m_proxyPort = port; + } + + int getProxyPort() const { + return m_proxyPort; + } + + std::list< RefPtr< Cookie > > getCookies() const { + return m_cookies; + } + + RefPtr<Response> get(RefPtr<Request> req) const; + + RefPtr<Response> post(RefPtr<Request> req) const; + + private: + std::string m_userAgentName; + std::string m_proxyHost; + int m_proxyPort; + std::list< RefPtr< Cookie > > m_cookies; + + +}; + +}; // namespace Http + +}; // namespace Net + +}; // namespace Tiki + +#endif // __TIKI_NET_HTTP_USERAGENT_H Modified: tiki/sdl/Makefile =================================================================== --- tiki/sdl/Makefile 2007-08-21 16:14:06 UTC (rev 481) +++ tiki/sdl/Makefile 2007-08-21 23:56:11 UTC (rev 482) @@ -11,6 +11,7 @@ BASE_MATH_OBJ=$(patsubst %.cpp,%.o,$(wildcard ../src/math/*.cpp)) BASE_THREAD_OBJ=$(patsubst %.cpp,%.o,$(wildcard ../src/thread/*.cpp)) BASE_NET_OBJ=$(patsubst %.cpp,%.o,$(wildcard ../src/net/*.cpp)) +BASE_NET_OBJ+=$(patsubst %.cpp,%.o,$(wildcard ../src/net/http/*.cpp)) JPEG_OBJ=$(patsubst %.c,%.o,$(wildcard ../3rdparty/libjpeg/*.c)) PNG_OBJ=$(patsubst %.c,%.o,$(wildcard ../3rdparty/libpng/*.c)) @@ -58,6 +59,10 @@ examples: $(MAKE) TIKI_PLAT=sdl TIKI_DIR=$(CURDIR)/../ -C$(CURDIR)/../examples/TikiTest $(MAKE) TIKI_PLAT=sdl TIKI_DIR=$(CURDIR)/../ -C$(CURDIR)/../examples/console/TikiSnake + $(MAKE) TIKI_PLAT=sdl TIKI_DIR=$(CURDIR)/../ -C$(CURDIR)/../examples/net/basic + $(MAKE) TIKI_PLAT=sdl TIKI_DIR=$(CURDIR)/../ -C$(CURDIR)/../examples/net/chatd + $(MAKE) TIKI_PLAT=sdl TIKI_DIR=$(CURDIR)/../ -C$(CURDIR)/../examples/net/chat + $(MAKE) TIKI_PLAT=sdl TIKI_DIR=$(CURDIR)/../ -C$(CURDIR)/../examples/net/httpclient package: tar -cvf ../dist/$(SVN_VERSION)/tiki-$(SVN_VERSION)-sdl.tar libtiki.a Added: tiki/src/net/http/request.cpp =================================================================== --- tiki/src/net/http/request.cpp (rev 0) +++ tiki/src/net/http/request.cpp 2007-08-21 23:56:11 UTC (rev 482) @@ -0,0 +1,87 @@ +/* + Tiki + + request.cpp + + Copyright (C)2007 Atani Software +*/ + +#include "Tiki/tiki.h" +#include "Tiki/net.h" +#include "Tiki/net/http/request.h" + +namespace Tiki { + +namespace Net { + +namespace Http { + +std::string DEFAULT_CONTENT_PART = "__TIKI__DEFAULT__"; + +Request::Request() { + setHeaderParam("Connection", "close"); +} + +void Request::setHeaderParam(std::string param, std::string value) { +// if(m_params.find(param) != m_params.end()) { +// m_params.erase(m_params.find(param)); +// } + + m_params.insert(std::make_pair(param, value)); +} + +std::string Request::getHeaderParam(std::string param) const { + if(m_params.find(param) != m_params.end()) { + return m_params.find(param)->second; + } + return ""; +} + +std::list<std::string> Request::getHeaderParamNames() const { + std::list<std::string> params; + + for( StringStringMap::const_iterator param = m_params.begin(); + param != m_params.end(); + ++param) { + + params.push_back(param->first); + } + + return params; +} + +void Request::addContentPart(std::string name, RefPtr<Buffer> input) { + //if(m_parts.find(name) != m_parts.end()) { + // m_parts.erase(m_parts.find(name)); + //} + + m_parts.insert(std::make_pair(name, input)); +} + +RefPtr<Buffer> Request::getContentPart(std::string name) const { + if(m_parts.find(name) != m_parts.end()) { + return m_parts.find(name)->second; + } + return NULL; +} + +std::list<std::string> Request::getContentPartNames() const { + std::list<std::string> parts; + + for( StringBufferMap::const_iterator part = m_parts.begin(); + part != m_parts.end(); + ++part) { + + parts.push_back(part->first); + } + + return parts; +} + + +}; // namespace Http + +}; // namespace Net + +}; // namespace Tiki + Added: tiki/src/net/http/response.cpp =================================================================== --- tiki/src/net/http/response.cpp (rev 0) +++ tiki/src/net/http/response.cpp 2007-08-21 23:56:11 UTC (rev 482) @@ -0,0 +1,28 @@ +/* + Tiki + + response.cpp + + Copyright (C)2007 Atani Software +*/ + +#include "Tiki/tiki.h" +#include "Tiki/net.h" +#include "Tiki/net/http/request.h" +#include "Tiki/net/http/response.h" + +namespace Tiki { + +namespace Net { + +namespace Http { + +Response::Response() { +} + +}; // namespace Http + +}; // namespace Net + +}; // namespace Tiki + Added: tiki/src/net/http/useragent.cpp =================================================================== --- tiki/src/net/http/useragent.cpp (rev 0) +++ tiki/src/net/http/useragent.cpp 2007-08-21 23:56:11 UTC (rev 482) @@ -0,0 +1,252 @@ +/* + Tiki + + useragent.cpp + + Copyright (C)2007 Atani Software +*/ + +#include "Tiki/tiki.h" +#include "Tiki/net.h" +#include "Tiki/net/http/useragent.h" + +#include <sstream> + +namespace Tiki { + +namespace Net { + +namespace Http { + +using namespace Tiki::Net::TCP; + +#define READ_ONE_LINE(res, socket) \ + { \ + RefPtr<Buffer> recvBuf = new Buffer(1); \ + char tbuf[2]; \ + res = ""; \ + while(socket->isOpen()) { \ + recvBuf->reset(); \ + socket->recv(recvBuf); \ + if(recvBuf->getUsedDataLen() > 0) { \ + tbuf[0] = recvBuf->getData()[0]; \ + tbuf[1] = '\0'; \ + if(tbuf[0] != '\n' && tbuf[0] != '\r' ) { \ + res.append((char *)tbuf); \ + } \ + else if(tbuf[0] != '\r' ) { \ + break; \ + } \ + } \ + } \ + } + +HttpUserAgent::HttpUserAgent() { + setUserAgentName("Tiki/1.0"); + setProxyHost(""); + setProxyPort(8080); +} + +RefPtr<Response> HttpUserAgent::get(RefPtr<Request> req) const { + RefPtr<TCPSocket> socket = new TCPSocket(); + socket->setNonBlocking(false); + RefPtr<Response> response = new Response(); + response->setUrl(req->getUrl()); + response->setResultCode(200); + + std::string url = req->getUrl(); + std::string hostname = ""; + int port = 80; + std::string resource = "/index.html"; + + if(url.find("http://") == 0) { + url = url.substr(std::string("http://").length()); + } + else if(url.find("https://") == 0) { + url = url.substr(std::string("https://").length()); + } + hostname = url.substr(0, url.find("/")); + if(url.find("/") != std::string::npos) { + resource = url.substr(url.find("/")); + } + if(hostname.find(":") != std::string::npos) { + std::string portstr = url.substr(hostname.find(":")); + port = atoi(portstr.c_str()); + hostname = hostname.substr(0, hostname.find(":")); + } + + RefPtr<Address> requestAddress = new Address(); + + std::stringstream request; + if(m_proxyHost.empty()) { + request << "GET " << resource << " HTTP/1.1\r\n"; + request << "Host: " << hostname << "\r\n"; + requestAddress->setHostName(hostname); + requestAddress->setPort(port); + } + else { + // proxy connection + request << "GET " << req->getUrl(); + if(port != 80) { + request << ":" << port; + } + request << " HTTP/1.1\r\n"; + request << "Host: " << hostname << "\r\n"; + requestAddress->setHostName(m_proxyHost); + requestAddress->setPort(m_proxyPort); + } + std::list<std::string> params = req->getHeaderParamNames(); + for(std::list<string>::const_iterator param = params.begin(); + param != params.end(); + ++param) { + request << *param << ": " << req->getHeaderParam(*param) << "\r\n"; + } + request << "\r\n"; + socket->setPeerAddress(requestAddress); + Tiki::Debug::printf("Request:\n%s", request.str().c_str()); + + socket->open(); + if(socket->isOpen()) { + Tiki::Debug::printf("Sending request...\n"); + } + else { + Tiki::Debug::printf("connect failed\n"); + response->setResultCode(504); + return response; + } + + RefPtr<Buffer> buf = new Buffer(2048); + std::string requeststr = request.str(); + buf->setData((uint8 *)requeststr.c_str(), requeststr.size()); + socket->send(buf); + + 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++) { + if(status.at(i) == ' ') { + response->setResultCode(atoi(status.c_str()+i + 1)); + break; + } + } + + while(1) { + std::string line = ""; + READ_ONE_LINE(line, socket) + if(line.size() == 0) { + // done with headers + break; + } + if(line.find(":") != std::string::npos) { + std::string field = line.substr(0, line.find(":")); + std::string value = line.substr(line.find(":") + 1); + while(value.at(0) == ' ') { + value = value.substr(1); + } + + if(!field.compare("Set-Cookie")) { + + } + else { + response->setHeaderParam(field, value); + } + } + } + + RefPtr<Buffer> fullBuf = new Buffer(1); + + if(!response->getHeaderParam("Transfer-Encoding").compare("chunked")) { + Tiki::Debug::printf("Encoding is chunked\n"); + // evil chunked encoding + size_t totalSize = 0; + + long sizeDecoded = 0; + do + { + sizeDecoded = 0; + std::string size = ""; + READ_ONE_LINE(size, socket); + if(size.size() == 0) { + sizeDecoded = 1; + continue; + } + + int base = 1; + for(int i = 0; i < size.size(); i++ ) { + char ch = size.at(i); + if(ch >= 'A' && ch <= 'F') { + ch -= 'A'; + ch += 10; + } + else if(ch >= 'a' && ch <= 'f') { + ch -= 'a'; + ch += 10; + } + else if(ch >= '0' && ch <= '9') { + ch -= '0'; + } + else { + continue; + } + sizeDecoded += (ch * pow(16.0f, size.size() - i - 1)); + } + //Tiki::Debug::printf("chunk size: %d [%s]\n", sizeDecoded, size.c_str()); + if(sizeDecoded > 0) { + + RefPtr<Buffer> chunkBuf = new Buffer(sizeDecoded); + socket->recv(chunkBuf); + if(chunkBuf->getUsedDataLen() < sizeDecoded) + { + //Tiki::Debug::printf("Buffer underflow\n"); + int needed = sizeDecoded - chunkBuf->getUsedDataLen(); + while(needed > 0) { + RefPtr<Buffer> chunkBuf2 = new Buffer(needed); + socket->recv(chunkBuf2); + chunkBuf->append(chunkBuf2); + needed -= chunkBuf2->getUsedDataLen(); + } + } + totalSize += chunkBuf->getUsedDataLen(); + fullBuf->append(chunkBuf); + } + } while(sizeDecoded > 0); + 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); + + RefPtr<Buffer> chunkBuf = new Buffer(sizeDecoded); + socket->recv(chunkBuf); + if(chunkBuf->getUsedDataLen() < sizeDecoded) + { + sizeDecoded -= chunkBuf->getUsedDataLen(); + while(sizeDecoded > 0) { + RefPtr<Buffer> chunkBuf2 = new Buffer(sizeDecoded); + socket->recv(chunkBuf2); + chunkBuf->append(chunkBuf2); + sizeDecoded -= chunkBuf2->getUsedDataLen(); + } + } + fullBuf->append(chunkBuf); + } + else { + Tiki::Debug::printf("Encoding is unknown\n"); + } + response->addContentPart(DEFAULT_CONTENT_PART, fullBuf); + + return response; +} + +RefPtr<Response> HttpUserAgent::post(RefPtr<Request> req) const { + return NULL; +} + +}; // namespace Http + +}; // namespace Net + +}; // namespace Tiki + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |