00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifdef _MSC_VER
00034 #pragma warning(disable:4786)
00035 #endif
00036 #include <stdarg.h>
00037 #include <stdio.h>
00038
00039 #include "HttpResponse.h"
00040 #include "HttpRequest.h"
00041 #include "MemFile.h"
00042 #include "File.h"
00043 #include "Parse.h"
00044
00045 #ifdef SOCKETS_NAMESPACE
00046 namespace SOCKETS_NAMESPACE {
00047 #endif
00048
00049 #ifdef _DEBUG
00050 #define DEB(x) x; fflush(stderr);
00051 #else
00052 #define DEB(x)
00053 #endif
00054
00055
00056
00057 HttpResponse::HttpResponse(const std::string& version) : HttpTransaction()
00058 , m_http_version( version )
00059 , m_http_status_code(0)
00060 , m_file( new MemFile )
00061 {
00062 }
00063
00064
00065
00066 HttpResponse::HttpResponse(const HttpResponse& src) : HttpTransaction(src)
00067 , m_http_version( src.m_http_version )
00068 , m_http_status_code( src.m_http_status_code )
00069 , m_http_status_msg( src.m_http_status_msg )
00070 , m_cookie( src.m_cookie )
00071 , m_file( src.m_file )
00072 {
00073 }
00074
00075
00076
00077 HttpResponse::~HttpResponse()
00078 {
00079 }
00080
00081
00082
00083 HttpResponse& HttpResponse::operator=(const HttpResponse& src)
00084 {
00085 m_http_version = src.m_http_version;
00086 m_http_status_code = src.m_http_status_code;
00087 m_http_status_msg = src.m_http_status_msg;
00088 m_cookie = src.m_cookie;
00089 m_file = src.m_file;
00090
00091 HttpTransaction::operator=(src);
00092
00093 return *this;
00094 }
00095
00096
00097
00098 void HttpResponse::SetHttpVersion(const std::string& value)
00099 {
00100 m_http_version = value;
00101 }
00102
00103
00104
00105 const std::string& HttpResponse::HttpVersion() const
00106 {
00107 return m_http_version;
00108 }
00109
00110
00111
00112
00113 void HttpResponse::SetHttpStatusCode(int value)
00114 {
00115 m_http_status_code = value;
00116 }
00117
00118
00119 int HttpResponse::HttpStatusCode() const
00120 {
00121 return m_http_status_code;
00122 }
00123
00124
00125
00126
00127 void HttpResponse::SetHttpStatusMsg(const std::string& value)
00128 {
00129 m_http_status_msg = value;
00130 }
00131
00132
00133 const std::string& HttpResponse::HttpStatusMsg() const
00134 {
00135 return m_http_status_msg;
00136 }
00137
00138
00139
00140 void HttpResponse::SetCookie(const std::string& value)
00141 {
00142 Parse pa(value, "=");
00143 std::string name = pa.getword();
00144 m_cookie[name] = value;
00145 DEB(fprintf(stderr, "HttpResponse::Set-Cookie<%s>: %s\n", name.c_str(), value.c_str());)
00146 }
00147
00148
00149 const std::string HttpResponse::Cookie(const std::string& name) const
00150 {
00151 Utility::ncmap<std::string>::const_iterator it = m_cookie.find(name);
00152 DEB(fprintf(stderr, "HttpResponse; get value of Cookie<%s>: ", name.c_str());)
00153 if (it != m_cookie.end())
00154 {
00155 DEB(fprintf(stderr, "%s\n", it -> second.c_str());)
00156 return it -> second;
00157 }
00158 DEB(fprintf(stderr, "\n");)
00159 return "";
00160 }
00161
00162
00163 std::list<std::string> HttpResponse::CookieNames() const
00164 {
00165 std::list<std::string> vec;
00166 DEB(fprintf(stderr, "HttpResponse::CookieNames; ");)
00167 for (Utility::ncmap<std::string>::const_iterator it = m_cookie.begin(); it != m_cookie.end(); ++it)
00168 {
00169 DEB(fprintf(stderr, " %s", it -> first.c_str());)
00170 vec.push_back(it -> first);
00171 }
00172 DEB(fprintf(stderr, "\n");)
00173 return vec;
00174 }
00175
00176
00177
00178
00179 void HttpResponse::Write( const std::string& str )
00180 {
00181 Write( str.c_str(), str.size() );
00182 }
00183
00184
00185
00186 void HttpResponse::Write( const char *buf, size_t sz )
00187 {
00188 m_file -> fwrite( buf, 1, sz );
00189 }
00190
00191
00192
00193 void HttpResponse::Writef( const char *format, ... )
00194 {
00195 va_list ap;
00196 va_start(ap, format);
00197 char tmp[10000];
00198 vsnprintf(tmp, sizeof(tmp), format, ap);
00199 va_end(ap);
00200 m_file -> fwrite( tmp, 1, strlen(tmp) );
00201 }
00202
00203
00204
00205 const IFile& HttpResponse::GetFile() const
00206 {
00207 return *m_file;
00208 }
00209
00210
00211
00212 IFile& HttpResponse::GetFile()
00213 {
00214 return *m_file;
00215 }
00216
00217
00218
00219 void HttpResponse::SetFile( const std::string& path )
00220 {
00221 m_file = std::auto_ptr<IFile>(new File);
00222 m_file -> fopen( path, "rb" );
00223 }
00224
00225
00226
00227 void HttpResponse::SetFile( IFile *f )
00228 {
00229 m_file = std::auto_ptr<IFile>(f);
00230 }
00231
00232
00233
00234 void HttpResponse::Reset()
00235 {
00236 HttpTransaction::Reset();
00237 m_http_version = "";
00238 m_http_status_code = 0;
00239 m_http_status_msg = "";
00240 while (!m_cookie.empty())
00241 {
00242 m_cookie.erase(m_cookie.begin());
00243 }
00244 m_file = std::auto_ptr<IFile>(new MemFile);
00245 }
00246
00247
00248 #ifdef SOCKETS_NAMESPACE
00249 }
00250 #endif
00251