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 "AjpBaseSocket.h"
00037 #include "ajp13.h"
00038
00039 #ifdef SOCKETS_NAMESPACE
00040 namespace SOCKETS_NAMESPACE {
00041 #endif
00042
00043 #ifdef _DEBUG
00044 #define DEB(x) x
00045 #else
00046 #define DEB(x)
00047 #endif
00048
00049
00050
00051 AjpBaseSocket::Initializer AjpBaseSocket::Init;
00052
00053
00054
00055 AjpBaseSocket::Initializer::Initializer()
00056 {
00057
00058 Header[HTTP_REQUEST_ACCEPT] = "accept";
00059 Header[HTTP_REQUEST_ACCEPT_CHARSET] = "accept-charset";
00060 Header[HTTP_REQUEST_ACCEPT_ENCODING] = "accept-encoding";
00061 Header[HTTP_REQUEST_ACCEPT_LANGUAGE] = "accept-language";
00062 Header[HTTP_REQUEST_AUTHORIZATION] = "authorization";
00063 Header[HTTP_REQUEST_CONNECTION] = "connection";
00064 Header[HTTP_REQUEST_CONTENT_TYPE] = "content-type";
00065 Header[HTTP_REQUEST_CONTENT_LENGTH] = "content-length";
00066 Header[HTTP_REQUEST_COOKIE] = "cookie";
00067 Header[HTTP_REQUEST_COOKIE2] = "cookie2";
00068 Header[HTTP_REQUEST_HOST] = "host";
00069 Header[HTTP_REQUEST_PRAGMA] = "pragma";
00070 Header[HTTP_REQUEST_REFERER] = "referer";
00071 Header[HTTP_REQUEST_USER_AGENT] = "user-agent";
00072
00073 Method[HTTP_METHOD_OPTIONS] = "OPTIONS";
00074 Method[HTTP_METHOD_GET] = "GET";
00075 Method[HTTP_METHOD_HEAD] = "HEAD";
00076 Method[HTTP_METHOD_POST] = "POST";
00077 Method[HTTP_METHOD_PUT] = "PUT";
00078 Method[HTTP_METHOD_DELETE] = "DELETE";
00079 Method[HTTP_METHOD_TRACE] = "TRACE";
00080 Method[HTTP_METHOD_PROPFIND] = "PROPFIND";
00081 Method[HTTP_METHOD_PROPPATCH] = "PROPPATCH";
00082 Method[HTTP_METHOD_MKCOL] = "MKCOL";
00083 Method[HTTP_METHOD_COPY] = "COPY";
00084 Method[HTTP_METHOD_MOVE] = "MOVE";
00085 Method[HTTP_METHOD_LOCK] = "LOCK";
00086 Method[HTTP_METHOD_UNLOCK] = "UNLOCK";
00087 Method[HTTP_METHOD_ACL] = "ACL";
00088 Method[HTTP_METHOD_REPORT] = "REPORT";
00089 Method[HTTP_METHOD_VERSION_CONTROL] = "VERSION_CONTROL";
00090 Method[HTTP_METHOD_CHECKIN] = "CHECKIN";
00091 Method[HTTP_METHOD_CHECKOUT] = "CHECKOUT";
00092 Method[HTTP_METHOD_UNCHECKOUT] = "UNCHECKOUT";
00093 Method[HTTP_METHOD_SEARCH] = "SEARCH";
00094 Method[HTTP_METHOD_MKWORKSPACE] = "MKWORKSPACE";
00095 Method[HTTP_METHOD_UPDATE] = "UPDATE";
00096 Method[HTTP_METHOD_LABEL] = "LABEL";
00097 Method[HTTP_METHOD_MERGE] = "MERGE";
00098 Method[HTTP_METHOD_BASELINE_CONTROL] = "BASELINE_CONTROL";
00099 Method[HTTP_METHOD_MKACTIVITY] = "MKACTIVITY";
00100
00101 Attribute[ATTR_CONTEXT] = "context";
00102 Attribute[ATTR_SERVLET_PATH] = "servlet_path";
00103 Attribute[ATTR_REMOTE_USER] = "remote_user";
00104 Attribute[ATTR_AUTH_TYPE] = "auth_type";
00105 Attribute[ATTR_QUERY_STRING] = "query_string";
00106 Attribute[ATTR_ROUTE] = "route";
00107 Attribute[ATTR_SSL_CERT] = "ssl_cert";
00108 Attribute[ATTR_SSL_CIPHER] = "ssl_cipher";
00109 Attribute[ATTR_SSL_SESSION] = "ssl_session";
00110 Attribute[ATTR_SSL_KEY_SIZE] = "ssl_key_size";
00111 Attribute[ATTR_SECRET] = "secret";
00112 Attribute[ATTR_STORED_METHOD] = "stored_method";
00113
00114 ResponseHeader["content-type"] = 0xa001;
00115 ResponseHeader["content-language"] = 0xa002;
00116 ResponseHeader["content-length"] = 0xa003;
00117 ResponseHeader["date"] = 0xa004;
00118 ResponseHeader["last-modified"] = 0xa005;
00119 ResponseHeader["location"] = 0xa006;
00120 ResponseHeader["set-cookie"] = 0xa007;
00121 ResponseHeader["set-cookie2"] = 0xa008;
00122 ResponseHeader["servlet-engine"] = 0xa009;
00123 ResponseHeader["status"] = 0xa00a;
00124 ResponseHeader["www-authenticate"] = 0xa00b;
00125 }
00126
00127
00128
00129 AjpBaseSocket::AjpBaseSocket(ISocketHandler& h) : TcpSocket(h)
00130 , m_state(0)
00131 , m_length(4)
00132 , m_ptr(0)
00133 {
00134 }
00135
00136
00137
00138 void AjpBaseSocket::OnRawData(const char *buf, size_t sz)
00139 {
00140 DEB(fprintf(stderr, "OnRawData: %d bytes\n", sz);)
00141 size_t ptr = 0;
00142 while (true)
00143 {
00144 size_t left = sz - ptr;
00145 DEB(fprintf(stderr, " left: %d bytes\n", left);
00146 fprintf(stderr, " state: %d\n", m_state);)
00147 switch (m_state)
00148 {
00149 case 0:
00150 {
00151 size_t missing = m_length - m_ptr;
00152 short len = (short)(missing < left ? missing : left);
00153 memcpy(m_message + m_ptr, buf + ptr, len);
00154 m_ptr += len;
00155 ptr += len;
00156 if (m_ptr < m_length)
00157 {
00158 return;
00159 }
00160 int p = 0;
00161 short id = get_integer(m_message, p);
00162 short length = get_integer(m_message, p);
00163 OnHeader(id, length);
00164 m_state = 1;
00165 m_length = length;
00166 m_ptr = 0;
00167 }
00168 break;
00169 case 1:
00170 {
00171 size_t missing = m_length - m_ptr;
00172 short len = (short)(missing < left ? missing : left);
00173 memcpy(m_message + m_ptr, buf + ptr, len);
00174 m_ptr += len;
00175 ptr += len;
00176 if (m_ptr < m_length)
00177 {
00178 return;
00179 }
00180 OnPacket(m_message, m_ptr);
00181 m_state = 0;
00182 m_length = 4;
00183 m_ptr = 0;
00184 }
00185 break;
00186 }
00187 }
00188 }
00189
00190
00191
00192 unsigned char AjpBaseSocket::get_byte(const char *buf, int& ptr)
00193 {
00194 return (unsigned char)buf[ptr++];
00195 }
00196
00197
00198
00199 bool AjpBaseSocket::get_boolean(const char *buf, int& ptr)
00200 {
00201 return ( (unsigned char)buf[ptr++] & 1) == 1 ? true : false;
00202 }
00203
00204
00205
00206 short AjpBaseSocket::get_integer(const char *buf, int& ptr)
00207 {
00208 short n;
00209 memcpy(&n, buf + ptr, 2);
00210 ptr += 2;
00211 return ntohs(n);
00212 }
00213
00214
00215
00216 std::string AjpBaseSocket::get_string(const char *buf, int& ptr)
00217 {
00218 short len = get_integer(buf, ptr);
00219 if (len != -1)
00220 {
00221 std::string tmp = buf + ptr;
00222 ptr += len;
00223 ptr++;
00224 tmp.resize(len);
00225 return tmp;
00226 }
00227 return "";
00228 }
00229
00230
00231
00232 void AjpBaseSocket::put_byte(char *buf, int& ptr, unsigned char zz)
00233 {
00234 buf[ptr++] = zz;
00235 }
00236
00237
00238
00239 void AjpBaseSocket::put_boolean(char *buf, int& ptr, bool zz)
00240 {
00241 buf[ptr++] = zz ? 1 : 0;
00242 }
00243
00244
00245
00246 void AjpBaseSocket::put_integer(char *buf, int& ptr, short zz)
00247 {
00248 short tmp = htons(zz);
00249 memcpy(buf + ptr, &tmp, 2);
00250 ptr += 2;
00251 }
00252
00253
00254
00255 void AjpBaseSocket::put_string(char *buf, int& ptr, const std::string& zz)
00256 {
00257 put_integer(buf, ptr, (short)zz.size() );
00258 memcpy(buf + ptr, zz.c_str(), zz.size());
00259 ptr += (int)zz.size();
00260 put_byte(buf, ptr, 0);
00261 }
00262
00263
00264
00265 void AjpBaseSocket::reset()
00266 {
00267 m_state = m_ptr = 0;
00268 m_length = 4;
00269 }
00270
00271
00272 #ifdef SOCKETS_NAMESPACE
00273 }
00274 #endif
00275
00276