svn cleanup
[anytun.git] / Sockets / HttpGetSocket.cpp
blobfe5e3d5c1df90884aabea6a0899761391e0af6d0
1 /** \file HttpGetSocket.cpp
2 ** \date 2004-02-13
3 ** \author grymse@alhem.net
4 **/
5 /*
6 Copyright (C) 2004-2007 Anders Hedstrom
8 This library is made available under the terms of the GNU GPL.
10 If you would like to use this library in a closed-source application,
11 a separate license agreement is available. For information about
12 the closed-source license agreement for the C++ sockets library,
13 please visit http://www.alhem.net/Sockets/license.html and/or
14 email license@alhem.net.
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public License
18 as published by the Free Software Foundation; either version 2
19 of the License, or (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #ifdef _WIN32
31 #ifdef _MSC_VER
32 #pragma warning(disable:4786)
33 #endif
34 #else
35 #include <errno.h>
36 #endif
37 #include "Utility.h"
38 #include "Parse.h"
39 #include "ISocketHandler.h"
40 #include "HttpGetSocket.h"
43 #ifdef SOCKETS_NAMESPACE
44 namespace SOCKETS_NAMESPACE {
45 #endif
48 HttpGetSocket::HttpGetSocket(ISocketHandler& h) : HttpClientSocket(h)
53 HttpGetSocket::HttpGetSocket(ISocketHandler& h,const std::string& url_in,const std::string& to_file) : HttpClientSocket(h, url_in)
55 if (to_file.size())
57 SetFilename(to_file);
59 if (!Open(GetUrlHost(),GetUrlPort()))
61 if (!Connecting())
63 Handler().LogError(this, "HttpGetSocket", -1, "connect() failed miserably", LOG_LEVEL_FATAL);
64 SetCloseAndDelete();
70 HttpGetSocket::HttpGetSocket(ISocketHandler& h,const std::string& host,port_t port,const std::string& url,const std::string& to_file) : HttpClientSocket(h)
72 SetUrl(url);
73 if (to_file.size())
75 SetFilename(to_file);
77 if (!Open(host, port))
79 if (!Connecting())
81 Handler().LogError(this, "HttpGetSocket", -1, "connect() failed miserably", LOG_LEVEL_FATAL);
82 SetCloseAndDelete();
88 HttpGetSocket::~HttpGetSocket()
93 void HttpGetSocket::OnConnect()
95 SetMethod( "GET" );
96 AddResponseHeader( "Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1");
97 AddResponseHeader( "Accept-Language", "en-us,en;q=0.5");
98 AddResponseHeader( "Accept-Encoding", "gzip,deflate");
99 AddResponseHeader( "Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
100 AddResponseHeader( "User-agent", MyUseragent() );
102 if (GetUrlPort() != 80 && GetUrlPort() != 443)
103 AddResponseHeader( "Host", GetUrlHost() + ":" + Utility::l2string(GetUrlPort()) );
104 else
105 AddResponseHeader( "Host", GetUrlHost() );
106 SendRequest();
110 #ifdef SOCKETS_NAMESPACE
112 #endif