another test
[anytun.git] / Sockets / HttpResponse.cpp
blobc312ee23bc1e40d00969a91496d6f44a0101338e
1 /**
2 ** \file HttpResponse.cpp
3 ** \date 2007-10-05
4 ** \author grymse@alhem.net
5 **/
6 /*
7 Copyright (C) 2007 Anders Hedstrom
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #ifdef _MSC_VER
24 #pragma warning(disable:4786)
25 #endif
26 #include <stdarg.h>
27 #include <stdio.h>
29 #include "HttpResponse.h"
30 #include "HttpRequest.h"
31 #include "MemFile.h"
32 #include "File.h"
33 #include "Parse.h"
34 #include "Utility.h"
36 #ifdef SOCKETS_NAMESPACE
37 namespace SOCKETS_NAMESPACE {
38 #endif
40 #ifdef _DEBUG
41 #define DEB(x) x; fflush(stderr);
42 #else
43 #define DEB(x)
44 #endif
47 // --------------------------------------------------------------------------------------
48 HttpResponse::HttpResponse(const std::string& version) : HttpTransaction()
49 , m_http_version( version )
50 , m_http_status_code(0)
51 , m_file( new MemFile )
56 // --------------------------------------------------------------------------------------
57 HttpResponse::~HttpResponse()
59 delete m_file;
63 // --------------------------------------------------------------------------------------
64 void HttpResponse::SetHttpVersion(const std::string& value)
66 m_http_version = value;
70 // --------------------------------------------------------------------------------------
71 const std::string& HttpResponse::HttpVersion() const
73 return m_http_version;
78 // --------------------------------------------------------------------------------------
79 void HttpResponse::SetHttpStatusCode(int value)
81 m_http_status_code = value;
85 int HttpResponse::HttpStatusCode() const
87 return m_http_status_code;
92 // --------------------------------------------------------------------------------------
93 void HttpResponse::SetHttpStatusMsg(const std::string& value)
95 m_http_status_msg = value;
99 const std::string& HttpResponse::HttpStatusMsg() const
101 return m_http_status_msg;
105 // --------------------------------------------------------------------------------------
106 void HttpResponse::SetCookie(const std::string& value)
108 Parse pa(value, "=");
109 std::string name = pa.getword();
110 m_cookie[Utility::ToLower(name)] = value;
111 DEB(fprintf(stderr, "HttpResponse::Set-Cookie<%s>: %s\n", name.c_str(), value.c_str());)
115 const std::string HttpResponse::Cookie(const std::string& name) const
117 std::map<std::string, std::string>::const_iterator it = m_cookie.find(name);
118 DEB(fprintf(stderr, "HttpResponse; get value of Cookie<%s>: ", name.c_str());)
119 if (it != m_cookie.end())
121 DEB(fprintf(stderr, "%s\n", it -> second.c_str());)
122 return it -> second;
124 DEB(fprintf(stderr, "\n");)
125 return "";
129 std::list<std::string> HttpResponse::CookieNames() const
131 std::list<std::string> vec;
132 DEB(fprintf(stderr, "HttpResponse::CookieNames; ");)
133 for (std::map<std::string, std::string>::const_iterator it = m_cookie.begin(); it != m_cookie.end(); it++)
135 DEB(fprintf(stderr, " %s", it -> first.c_str());)
136 vec.push_back(it -> first);
138 DEB(fprintf(stderr, "\n");)
139 return vec;
144 // --------------------------------------------------------------------------------------
145 void HttpResponse::Write( const std::string& str )
147 Write( str.c_str(), str.size() );
151 // --------------------------------------------------------------------------------------
152 void HttpResponse::Write( const char *buf, size_t sz )
154 m_file -> fwrite( buf, 1, sz );
158 // --------------------------------------------------------------------------------------
159 void HttpResponse::Writef( const char *format, ... )
161 va_list ap;
162 va_start(ap, format);
163 char tmp[10000];
164 vsprintf(tmp, format, ap);
165 va_end(ap);
166 m_file -> fwrite( tmp, 1, strlen(tmp) );
170 // --------------------------------------------------------------------------------------
171 void HttpResponse::SetFile( const std::string& path )
173 delete m_file;
174 m_file = new File();
175 m_file -> fopen( path, "rb" );
179 // --------------------------------------------------------------------------------------
180 void HttpResponse::Reset()
182 HttpTransaction::Reset();
183 m_http_version = "";
184 m_http_status_code = 0;
185 m_http_status_msg = "";
186 while (!m_cookie.empty())
188 m_cookie.erase(m_cookie.begin());
190 if (m_file)
192 delete m_file;
193 m_file = new MemFile;
198 #ifdef SOCKETS_NAMESPACE
199 } // namespace SOCKETS_NAMESPACE {
200 #endif