2 ** \file HttpResponse.cpp
4 ** \author grymse@alhem.net
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.
24 #pragma warning(disable:4786)
29 #include "HttpResponse.h"
30 #include "HttpRequest.h"
36 #ifdef SOCKETS_NAMESPACE
37 namespace SOCKETS_NAMESPACE
{
41 #define DEB(x) x; fflush(stderr);
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()
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());)
124 DEB(fprintf(stderr
, "\n");)
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");)
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
, ... )
162 va_start(ap
, format
);
164 vsprintf(tmp
, format
, ap
);
166 m_file
-> fwrite( tmp
, 1, strlen(tmp
) );
170 // --------------------------------------------------------------------------------------
171 void HttpResponse::SetFile( const std::string
& path
)
175 m_file
-> fopen( path
, "rb" );
179 // --------------------------------------------------------------------------------------
180 void HttpResponse::Reset()
182 HttpTransaction::Reset();
184 m_http_status_code
= 0;
185 m_http_status_msg
= "";
186 while (!m_cookie
.empty())
188 m_cookie
.erase(m_cookie
.begin());
193 m_file
= new MemFile
;
198 #ifdef SOCKETS_NAMESPACE
199 } // namespace SOCKETS_NAMESPACE {