Commit 4787c975 authored by Rohit Gupta's avatar Rohit Gupta

delete files with GPL License + modify build scripts

parent 73aa2657
......@@ -880,6 +880,7 @@ set(UTIL_SRC
${OPENAIR2_DIR}/UTIL/OMG/omg_hashtable.c
${OPENAIR2_DIR}/UTIL/OMG/rwalk.c
${OPENAIR2_DIR}/UTIL/OMG/rwp.c
${OPENAIR2_DIR}/UTIL/OMG/sumo.c
${OPENAIR2_DIR}/UTIL/OMG/socket_traci_OMG.c
${OPENAIR2_DIR}/UTIL/OMG/static.c
${OPENAIR2_DIR}/UTIL/OMG/steadystaterwp.c
......@@ -1797,7 +1798,6 @@ add_executable(oaisim_nos1
${OPENAIR_TARGETS}/SIMU/USER/oaisim.c
${OPENAIR2_DIR}/RRC/NAS/nas_config.c
${OPENAIR2_DIR}/RRC/NAS/rb_config.c
${OPENAIR2_DIR}/UTIL/OMG/sumo.c
${OPENAIR_TARGETS}/COMMON/create_tasks.c
${HW_SOURCE}
${TRANSPORT_SOURCE}
......@@ -1882,7 +1882,6 @@ foreach(myExe s1ap
aes128_cmac_encrypt
secu_knas_encrypt_eia2)
add_executable(test_${myExe}
${OPENAIR3_DIR}/TEST/test_util.c
${OPENAIR3_DIR}/TEST/test_${myExe}.c
)
target_link_libraries (test_${myExe}
......
......@@ -6,20 +6,6 @@
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This header file may also be distributed under
* the terms of the BSD Licence as follows:
*
......
......@@ -25,7 +25,6 @@
* \date 2009
* \version 0.1
* \company Eurecom
* \note This conde is under GNU GENERAL PUBLIC LICENSE, version 2, June 1991
* \email: navid.nikaein@eurecom.fr
* \warning
*/
......
/**
** \file Ajp13Socket.h
** \date 2007-10-05
** \author grymse@alhem.net
**/
/*
Copyright (C) 2007-2010 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _SOCKETS_Ajp13Socket_H
#define _SOCKETS_Ajp13Socket_H
#include "AjpBaseSocket.h"
#include "HttpRequest.h"
#include "IHttpServer.h"
#include "HttpResponse.h"
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE
{
#endif
class HttpResponse;
class Ajp13Socket : public AjpBaseSocket, public IHttpServer
{
public:
Ajp13Socket(ISocketHandler& h);
void OnHeader( short id, short len );
void OnPacket( const char *buf, size_t sz );
// implements IHttpServer::Respond
void Respond(const HttpResponse& res);
void OnTransferLimit();
private:
void ReceiveBody( const char *buf, size_t sz );
void ReceiveForwardRequest( const char *buf, size_t sz );
void ReceiveShutdown( const char *buf, size_t sz );
void ReceivePing( const char *buf, size_t sz );
void ReceiveCPing( const char *buf, size_t sz );
void Execute();
//
size_t m_body_size_left;
HttpRequest m_req;
HttpResponse m_res;
};
#ifdef SOCKETS_NAMESPACE
} // namespace SOCKETS_NAMESPACE {
#endif
#endif // _SOCKETS_Ajp13Socket_H
/**
** \file AjpBaseSocket.cpp
** \date 2007-10-05
** \author grymse@alhem.net
**/
/*
Copyright (C) 2007-2010 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef _MSC_VER
#pragma warning(disable:4786)
#endif
#include "AjpBaseSocket.h"
#include "ajp13.h"
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE {
#endif
#ifdef _DEBUG
#define DEB(x) x
#else
#define DEB(x)
#endif
// ---------------------------------------------------------------------------
AjpBaseSocket::Initializer AjpBaseSocket::Init;
// ---------------------------------------------------------------------------
AjpBaseSocket::Initializer::Initializer()
{
Header[HTTP_REQUEST_ACCEPT] = "accept";
Header[HTTP_REQUEST_ACCEPT_CHARSET] = "accept-charset";
Header[HTTP_REQUEST_ACCEPT_ENCODING] = "accept-encoding";
Header[HTTP_REQUEST_ACCEPT_LANGUAGE] = "accept-language";
Header[HTTP_REQUEST_AUTHORIZATION] = "authorization";
Header[HTTP_REQUEST_CONNECTION] = "connection";
Header[HTTP_REQUEST_CONTENT_TYPE] = "content-type";
Header[HTTP_REQUEST_CONTENT_LENGTH] = "content-length";
Header[HTTP_REQUEST_COOKIE] = "cookie";
Header[HTTP_REQUEST_COOKIE2] = "cookie2";
Header[HTTP_REQUEST_HOST] = "host";
Header[HTTP_REQUEST_PRAGMA] = "pragma";
Header[HTTP_REQUEST_REFERER] = "referer";
Header[HTTP_REQUEST_USER_AGENT] = "user-agent";
Method[HTTP_METHOD_OPTIONS] = "OPTIONS";
Method[HTTP_METHOD_GET] = "GET";
Method[HTTP_METHOD_HEAD] = "HEAD";
Method[HTTP_METHOD_POST] = "POST";
Method[HTTP_METHOD_PUT] = "PUT";
Method[HTTP_METHOD_DELETE] = "DELETE";
Method[HTTP_METHOD_TRACE] = "TRACE";
Method[HTTP_METHOD_PROPFIND] = "PROPFIND";
Method[HTTP_METHOD_PROPPATCH] = "PROPPATCH";
Method[HTTP_METHOD_MKCOL] = "MKCOL";
Method[HTTP_METHOD_COPY] = "COPY";
Method[HTTP_METHOD_MOVE] = "MOVE";
Method[HTTP_METHOD_LOCK] = "LOCK";
Method[HTTP_METHOD_UNLOCK] = "UNLOCK";
Method[HTTP_METHOD_ACL] = "ACL";
Method[HTTP_METHOD_REPORT] = "REPORT";
Method[HTTP_METHOD_VERSION_CONTROL] = "VERSION_CONTROL"; // with a dash "VERSION-CONTROL"
Method[HTTP_METHOD_CHECKIN] = "CHECKIN";
Method[HTTP_METHOD_CHECKOUT] = "CHECKOUT";
Method[HTTP_METHOD_UNCHECKOUT] = "UNCHECKOUT";
Method[HTTP_METHOD_SEARCH] = "SEARCH";
Method[HTTP_METHOD_MKWORKSPACE] = "MKWORKSPACE";
Method[HTTP_METHOD_UPDATE] = "UPDATE";
Method[HTTP_METHOD_LABEL] = "LABEL";
Method[HTTP_METHOD_MERGE] = "MERGE";
Method[HTTP_METHOD_BASELINE_CONTROL] = "BASELINE_CONTROL";
Method[HTTP_METHOD_MKACTIVITY] = "MKACTIVITY";
Attribute[ATTR_CONTEXT] = "context";
Attribute[ATTR_SERVLET_PATH] = "servlet_path";
Attribute[ATTR_REMOTE_USER] = "remote_user";
Attribute[ATTR_AUTH_TYPE] = "auth_type";
Attribute[ATTR_QUERY_STRING] = "query_string";
Attribute[ATTR_ROUTE] = "route";
Attribute[ATTR_SSL_CERT] = "ssl_cert";
Attribute[ATTR_SSL_CIPHER] = "ssl_cipher";
Attribute[ATTR_SSL_SESSION] = "ssl_session";
Attribute[ATTR_SSL_KEY_SIZE] = "ssl_key_size";
Attribute[ATTR_SECRET] = "secret";
Attribute[ATTR_STORED_METHOD] = "stored_method";
ResponseHeader["content-type"] = 0xa001;
ResponseHeader["content-language"] = 0xa002;
ResponseHeader["content-length"] = 0xa003;
ResponseHeader["date"] = 0xa004;
ResponseHeader["last-modified"] = 0xa005;
ResponseHeader["location"] = 0xa006;
ResponseHeader["set-cookie"] = 0xa007;
ResponseHeader["set-cookie2"] = 0xa008;
ResponseHeader["servlet-engine"] = 0xa009;
ResponseHeader["status"] = 0xa00a;
ResponseHeader["www-authenticate"] = 0xa00b;
}
// ---------------------------------------------------------------------------
AjpBaseSocket::AjpBaseSocket(ISocketHandler& h) : TcpSocket(h)
, m_state(0)
, m_length(4)
, m_ptr(0)
{
}
// ---------------------------------------------------------------------------
void AjpBaseSocket::OnRawData(const char *buf, size_t sz)
{
DEB(fprintf(stderr, "OnRawData: %d bytes\n", sz);)
size_t ptr = 0;
while (true)
{
size_t left = sz - ptr;
DEB(fprintf(stderr, " left: %d bytes\n", left);
fprintf(stderr, " state: %d\n", m_state);)
switch (m_state)
{
case 0:
{
size_t missing = m_length - m_ptr;
short len = (short)(missing < left ? missing : left);
memcpy(m_message + m_ptr, buf + ptr, len);
m_ptr += len;
ptr += len;
if (m_ptr < m_length)
{
return; // read more
}
int p = 0;
short id = get_integer(m_message, p);
short length = get_integer(m_message, p);
OnHeader(id, length);
m_state = 1;
m_length = length;
m_ptr = 0; // bytes in m_message
}
break;
case 1:
{
size_t missing = m_length - m_ptr;
short len = (short)(missing < left ? missing : left);
memcpy(m_message + m_ptr, buf + ptr, len);
m_ptr += len;
ptr += len;
if (m_ptr < m_length)
{
return; // read more
}
OnPacket(m_message, m_ptr);
m_state = 0;
m_length = 4;
m_ptr = 0;
}
break;
}
}
}
// ---------------------------------------------------------------------------
unsigned char AjpBaseSocket::get_byte(const char *buf, int& ptr)
{
return (unsigned char)buf[ptr++];
}
// ---------------------------------------------------------------------------
bool AjpBaseSocket::get_boolean(const char *buf, int& ptr)
{
return ( (unsigned char)buf[ptr++] & 1) == 1 ? true : false;
}
// ---------------------------------------------------------------------------
short AjpBaseSocket::get_integer(const char *buf, int& ptr)
{
short n;
memcpy(&n, buf + ptr, 2);
ptr += 2;
return ntohs(n);
}
// ---------------------------------------------------------------------------
std::string AjpBaseSocket::get_string(const char *buf, int& ptr)
{
short len = get_integer(buf, ptr);
if (len != -1)
{
std::string tmp = buf + ptr;
ptr += len;
ptr++; // skip trailing 0x0
tmp.resize(len);
return tmp;
}
return "";
}
// ---------------------------------------------------------------------------
void AjpBaseSocket::put_byte(char *buf, int& ptr, unsigned char zz)
{
buf[ptr++] = zz;
}
// ---------------------------------------------------------------------------
void AjpBaseSocket::put_boolean(char *buf, int& ptr, bool zz)
{
buf[ptr++] = zz ? 1 : 0;
}
// ---------------------------------------------------------------------------
void AjpBaseSocket::put_integer(char *buf, int& ptr, short zz)
{
short tmp = htons(zz);
memcpy(buf + ptr, &tmp, 2);
ptr += 2;
}
// ---------------------------------------------------------------------------
void AjpBaseSocket::put_string(char *buf, int& ptr, const std::string& zz)
{
put_integer(buf, ptr, (short)zz.size() );
memcpy(buf + ptr, zz.c_str(), zz.size());
ptr += (int)zz.size();
put_byte(buf, ptr, 0);
}
#ifdef SOCKETS_NAMESPACE
} // namespace SOCKETS_NAMESPACE {
#endif
/**
** \file AjpBaseSocket.h
** \date 2007-10-05
** \author grymse@alhem.net
**/
/*
Copyright (C) 2007-2010 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _SOCKETS_AjpBaseSocket_H
#define _SOCKETS_AjpBaseSocket_H
#include "TcpSocket.h"
#include <map>
#include "Utility.h"
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE
{
#endif
class AjpBaseSocket : public TcpSocket
{
class Initializer
{
public:
Initializer();
virtual ~Initializer() {}
std::map<int, std::string> Method;
std::map<int, std::string> Header;
std::map<int, std::string> Attribute;
Utility::ncmap<int> ResponseHeader;
};
public:
AjpBaseSocket(ISocketHandler& h);
void OnRawData(const char *buf, size_t sz);
virtual void OnHeader( short id, short len ) = 0;
virtual void OnPacket( const char *buf, size_t sz ) = 0;
protected:
unsigned char get_byte(const char *buf, int& ptr);
bool get_boolean(const char *buf, int& ptr);
short get_integer(const char *buf, int& ptr);
std::string get_string(const char *buf, int& ptr);
void put_byte(char *buf, int& ptr, unsigned char zz);
void put_boolean(char *buf, int& ptr, bool zz);
void put_integer(char *buf, int& ptr, short zz);
void put_string(char *buf, int& ptr, const std::string& zz);
static Initializer Init;
private:
int m_state;
int m_length;
int m_ptr;
char m_message[8192];
};
#ifdef SOCKETS_NAMESPACE
} // namespace SOCKETS_NAMESPACE {
#endif
#endif // _SOCKETS_AjpBaseSocket_H
/** \file Base64.cpp
** \date 2004-02-13
** \author grymse@alhem.net
**/
/*
Copyright (C) 2004-2010 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "Base64.h"
#include "IFile.h"
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE {
#endif
const char *Base64::bstr =
"ABCDEFGHIJKLMNOPQ"
"RSTUVWXYZabcdefgh"
"ijklmnopqrstuvwxy"
"z0123456789+/";
const char Base64::rstr[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0,
0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0,
0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0};
Base64::Base64()
{
}
void Base64::encode(IFile *fil, std::string& output, bool add_crlf)
{
size_t remain;
size_t i = 0;
size_t o = 0;
char input[4];
output = "";
remain = fil -> fread(input,1,3);
while (remain > 0)
{
if (add_crlf && o && o % 76 == 0)
output += "\n";
switch (remain)
{
case 1:
output += bstr[ ((input[i] >> 2) & 0x3f) ];
output += bstr[ ((input[i] << 4) & 0x30) ];
output += "==";
break;
case 2:
output += bstr[ ((input[i] >> 2) & 0x3f) ];
output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
output += "=";
break;
default:
output += bstr[ ((input[i] >> 2) & 0x3f) ];
output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
output += bstr[ (input[i + 2] & 0x3f) ];
}
o += 4;
//
remain = fil -> fread(input,1,3);
}
}
void Base64::encode(FILE *fil, std::string& output, bool add_crlf)
{
size_t remain;
size_t i = 0;
size_t o = 0;
char input[4];
output = "";
remain = fread(input,1,3,fil);
while (remain > 0)
{
if (add_crlf && o && o % 76 == 0)
output += "\n";
switch (remain)
{
case 1:
output += bstr[ ((input[i] >> 2) & 0x3f) ];
output += bstr[ ((input[i] << 4) & 0x30) ];
output += "==";
break;
case 2:
output += bstr[ ((input[i] >> 2) & 0x3f) ];
output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
output += "=";
break;
default:
output += bstr[ ((input[i] >> 2) & 0x3f) ];
output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
output += bstr[ (input[i + 2] & 0x3f) ];
}
o += 4;
//
remain = fread(input,1,3,fil);
}
}
void Base64::encode(const std::string& str_in, std::string& str_out, bool add_crlf)
{
encode(str_in.c_str(), str_in.size(), str_out, add_crlf);
}
void Base64::encode(const char* input,size_t l,std::string& output, bool add_crlf)
{
size_t i = 0;
size_t o = 0;
output = "";
while (i < l)
{
size_t remain = l - i;
if (add_crlf && o && o % 76 == 0)
output += "\n";
switch (remain)
{
case 1:
output += bstr[ ((input[i] >> 2) & 0x3f) ];
output += bstr[ ((input[i] << 4) & 0x30) ];
output += "==";
break;
case 2:
output += bstr[ ((input[i] >> 2) & 0x3f) ];
output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
output += "=";
break;
default:
output += bstr[ ((input[i] >> 2) & 0x3f) ];
output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
output += bstr[ (input[i + 2] & 0x3f) ];
}
o += 4;
i += 3;
}
}
void Base64::encode(const unsigned char* input,size_t l,std::string& output,bool add_crlf)
{
size_t i = 0;
size_t o = 0;
output = "";
while (i < l)
{
size_t remain = l - i;
if (add_crlf && o && o % 76 == 0)
output += "\n";
switch (remain)
{
case 1:
output += bstr[ ((input[i] >> 2) & 0x3f) ];
output += bstr[ ((input[i] << 4) & 0x30) ];
output += "==";
break;
case 2:
output += bstr[ ((input[i] >> 2) & 0x3f) ];
output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
output += bstr[ ((input[i + 1] << 2) & 0x3c) ];
output += "=";
break;
default:
output += bstr[ ((input[i] >> 2) & 0x3f) ];
output += bstr[ ((input[i] << 4) & 0x30) + ((input[i + 1] >> 4) & 0x0f) ];
output += bstr[ ((input[i + 1] << 2) & 0x3c) + ((input[i + 2] >> 6) & 0x03) ];
output += bstr[ (input[i + 2] & 0x3f) ];
}
o += 4;
i += 3;
}
}
void Base64::decode(const std::string& input,std::string& output)
{
size_t i = 0;
size_t l = input.size();
output = "";
while (i < l)
{
while (i < l && (input[i] == 13 || input[i] == 10))
i++;
if (i < l)
{
char b1 = (char)((rstr[(int)input[i]] << 2 & 0xfc) +
(rstr[(int)input[i + 1]] >> 4 & 0x03));
output += b1;
if (input[i + 2] != '=')
{
char b2 = (char)((rstr[(int)input[i + 1]] << 4 & 0xf0) +
(rstr[(int)input[i + 2]] >> 2 & 0x0f));
output += b2;
}
if (input[i + 3] != '=')
{
char b3 = (char)((rstr[(int)input[i + 2]] << 6 & 0xc0) +
rstr[(int)input[i + 3]]);
output += b3;
}
i += 4;
}
}
}
void Base64::decode(const std::string& input, unsigned char *output, size_t& sz)
{
size_t i = 0;
size_t l = input.size();
size_t j = 0;
while (i < l)
{
while (i < l && (input[i] == 13 || input[i] == 10))
i++;
if (i < l)
{
unsigned char b1 = (unsigned char)((rstr[(int)input[i]] << 2 & 0xfc) +
(rstr[(int)input[i + 1]] >> 4 & 0x03));
if (output)
{
output[j] = b1;
}
j++;
if (input[i + 2] != '=')
{
unsigned char b2 = (unsigned char)((rstr[(int)input[i + 1]] << 4 & 0xf0) +
(rstr[(int)input[i + 2]] >> 2 & 0x0f));
if (output)
{
output[j] = b2;
}
j++;
}
if (input[i + 3] != '=')
{
unsigned char b3 = (unsigned char)((rstr[(int)input[i + 2]] << 6 & 0xc0) +
rstr[(int)input[i + 3]]);
if (output)
{
output[j] = b3;
}
j++;
}
i += 4;
}
}
sz = j;
}
size_t Base64::decode_length(const std::string& str64)
{
if (!str64.size() || str64.size() % 4)
return 0;
size_t l = 3 * (str64.size() / 4 - 1) + 1;
if (str64[str64.size() - 2] != '=')
l++;
if (str64[str64.size() - 1] != '=')
l++;
return l;
}
#ifdef SOCKETS_NAMESPACE
}
#endif
/** \file Base64.h
** \date 2004-02-13
** \author grymse@alhem.net
**/
/*
Copyright (C) 2004-2010 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _SOCKETS_Base64_H
#define _SOCKETS_Base64_H
#include "sockets-config.h"
#ifdef _MSC_VER
#pragma warning(disable:4514)
#endif
#include <stdio.h>
#include <string>
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE
{
#endif
class IFile;
/** \defgroup util Utilities */
/** Base64 encode/decode.
\ingroup util */
class Base64
{
public:
Base64();
void encode(FILE *, std::string& , bool add_crlf = true);
void encode(IFile *, std::string& , bool add_crlf = true);
void encode(const std::string&, std::string& , bool add_crlf = true);
void encode(const char *, size_t, std::string& , bool add_crlf = true);
void encode(const unsigned char *, size_t, std::string& , bool add_crlf = true);
void decode(const std::string&, std::string& );
void decode(const std::string&, unsigned char *, size_t&);
size_t decode_length(const std::string& );
private:
Base64(const Base64& ) {}
Base64& operator=(const Base64& ) {
return *this;
}
static const char *bstr;
static const char rstr[128];
};
#ifdef SOCKETS_NAMESPACE
}
#endif
#endif // _SOCKETS_Base64_H
#include "Debug.h"
#include <stdarg.h>
#include <cstring>
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE {
#endif
std::map<unsigned long, int> Debug::m_level;
const char *Debug::colors[] = {
"\x1B[0;0m", // &n */
"\x1B[0;0m\x1B[31m", // &r */
"\x1B[0;0m\x1B[32m", // &g */
"\x1B[0;0m\x1B[33m", // &y */
"\x1B[0;0m\x1B[34m", // &b */
"\x1B[0;0m\x1B[35m", // &m */
"\x1B[0;0m\x1B[36m", // &c */
"\x1B[0;0m\x1B[37m", // &w */
"\x1B[1;31m", // &R */
"\x1B[1;32m", // &G */
"\x1B[1;33m", // &Y */
"\x1B[1;34m", // &B */
"\x1B[1;35m", // &M */
"\x1B[1;36m", // &C */
"\x1B[1;37m" }; // &W */
void Debug::Print(const char *format, ...)
{
char slask[5000]; // temporary for vsprintf / vsnprintf
va_list ap;
va_start(ap, format);
vsnprintf(slask, sizeof(slask), format, ap);
va_end(ap);
fprintf(stderr, "%s", colors[Utility::ThreadID() % 14 + 1]);
for (int i = 0; i < m_level[Utility::ThreadID()]; i++)
fprintf(stderr, " ");
if (slask[strlen(slask) - 1] == '\n')
slask[strlen(slask) - 1] = 0;
fprintf(stderr, "%s%s\n", slask, colors[0]);
}
Debug& Debug::operator<<(const char * str)
{
m_line += str;
return *this;
}
Debug& Debug::operator<<(const std::string& str)
{
m_line += str;
return *this;
}
Debug& Debug::operator<<(short l)
{
m_line += Utility::l2string(l);
return *this;
}
Debug& Debug::operator<<(int l)
{
m_line += Utility::l2string(l);
return *this;
}
Debug& Debug::operator<<(long l)
{
m_line += Utility::l2string(l);
return *this;
}
Debug& Debug::operator<<(double d)
{
char slask[100];
snprintf(slask, sizeof(slask), "%f", d);
m_line += slask;
return *this;
}
Debug& Debug::operator<<(endl)
{
Print("%s", m_line.c_str());
m_line = "";
return *this;
}
#ifdef SOCKETS_NAMESPACE
} // namespace SOCKETS_NAMESPACE {
#endif
#ifndef _SOCKETS_Debug_H
#define _SOCKETS_Debug_H
#include "sockets-config.h"
#include "Utility.h"
#include <stdio.h>
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE
{
#endif
class Debug
{
static const char *colors[];
public:
class endl
{
public:
endl() {}
virtual ~endl() {}
};
public:
Debug() {}
Debug(const std::string& x) : m_id(0), m_text(x) {
fprintf(stderr, "%s", colors[Utility::ThreadID() % 14 + 1]);
for (int i = 0; i < m_level[Utility::ThreadID()]; i++)
fprintf(stderr, " ");
fprintf(stderr, "%s%s\n", x.c_str(), colors[0]);
m_level[Utility::ThreadID()]++;
}
Debug(int id, const std::string& x) : m_id(id), m_text(x) {
fprintf(stderr, "%s", colors[Utility::ThreadID() % 14 + 1]);
for (int i = 0; i < m_level[Utility::ThreadID()]; i++)
fprintf(stderr, " ");
fprintf(stderr, "%d> %s%s\n", m_id, x.c_str(), colors[0]);
m_level[Utility::ThreadID()]++;
}
~Debug() {
if (!m_text.empty()) {
if (m_level[Utility::ThreadID()])
m_level[Utility::ThreadID()]--;
fprintf(stderr, "%s", colors[Utility::ThreadID() % 14 + 1]);
for (int i = 0; i < m_level[Utility::ThreadID()]; i++)
fprintf(stderr, " ");
if (m_id)
fprintf(stderr, "%d> /%s%s\n", m_id, m_text.c_str(), colors[0]);
else
fprintf(stderr, "/%s%s\n", m_text.c_str(), colors[0]);
fflush(stderr);
}
}
static void Print(const char *format, ...);
Debug& operator<<(const char *);
Debug& operator<<(const std::string& );
Debug& operator<<(short);
Debug& operator<<(int);
Debug& operator<<(long);
Debug& operator<<(double);
Debug& operator<<(endl);
private:
int m_id;
std::string m_text;
static std::map<unsigned long, int> m_level;
std::string m_line;
};
#ifdef SOCKETS_NAMESPACE
} // namespace SOCKETS_NAMESPACE {
#endif
#endif // _SOCKETS_Debug_H
/** \file Event.cpp
** \date 2005-12-07
** \author grymse@alhem.net
**/
/*
Copyright (C) 2005-2010 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "Event.h"
#ifdef _WIN32
#else
#include <sys/time.h>
#endif
//#include "IEventOwner.h"
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE {
#endif
// statics
long Event::m_unique_id = 0;
Event::Event(IEventOwner *from,long sec,long usec,unsigned long data) : m_from(from), m_data(data), m_time(sec, usec), m_id(++m_unique_id)
{
}
Event::~Event()
{
}
bool Event::operator<(Event& e)
{
return m_time < e.m_time;
}
long Event::GetID() const
{
return m_id;
}
const EventTime& Event::GetTime() const
{
return m_time;
}
IEventOwner *Event::GetFrom() const
{
return m_from;
}
unsigned long Event::Data() const
{
return m_data;
}
#ifdef SOCKETS_NAMESPACE
}
#endif
/** \file Event.h
** \date 2005-12-07
** \author grymse@alhem.net
**/
/*
Copyright (C) 2005-2010 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _SOCKETS_Event_H
#define _SOCKETS_Event_H
#include "sockets-config.h"
#ifdef _WIN32
#else
#include <sys/select.h>
#endif
#include "EventTime.h"
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE
{
#endif
class IEventOwner;
/** Store information about a timer event.
\ingroup timer */
class Event
{
public:
Event(IEventOwner *,long sec,long usec,unsigned long data = 0);
~Event();
bool operator<(Event&);
long GetID() const;
const EventTime& GetTime() const;
IEventOwner *GetFrom() const;
unsigned long Data() const;
private:
Event(const Event& ) {} // copy constructor
Event& operator=(const Event& ) {
return *this; // assignment operator
}
IEventOwner *m_from;
unsigned long m_data;
EventTime m_time;
static long m_unique_id;
long m_id;
};
#ifdef SOCKETS_NAMESPACE
}
#endif
#endif // _SOCKETS_Event_H
/** \file EventHandler.cpp
** \date 2005-12-07
** \author grymse@alhem.net
**/
/*
Copyright (C) 2005-2010 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef _MSC_VER
#pragma warning(disable:4786)
#endif
#include "EventHandler.h"
#include "IEventOwner.h"
#include "Event.h"
#include "TcpSocket.h"
#include "ListenSocket.h"
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE {
#endif
EventHandler::EventHandler(StdLog *p) : SocketHandler(p), m_quit(false)
{
EnableRelease();
}
EventHandler::EventHandler(IMutex& m,StdLog *p) : SocketHandler(m, p), m_quit(false)
{
EnableRelease();
}
EventHandler::~EventHandler()
{
while (m_events.size())
{
std::list<Event *>::iterator it = m_events.begin();
Event *e = *it;
e -> GetFrom() -> SetHandlerInvalid();
delete e;
m_events.erase(it);
}
}
bool EventHandler::GetTimeUntilNextEvent(struct timeval *tv)
{
if (!m_events.size())
return false;
std::list<Event *>::iterator it = m_events.begin();
if (it != m_events.end())
{
EventTime now;
mytime_t diff = (*it) -> GetTime() - now;
if (diff < 1)
{
diff = 1;
}
tv -> tv_sec = static_cast<long>(diff / 1000000);
tv -> tv_usec = static_cast<long>(diff % 1000000);
return true;
}
return false;
}
void EventHandler::CheckEvents()
{
EventTime now;
std::list<Event *>::iterator it = m_events.begin();
while (it != m_events.end() && (*it) -> GetTime() < now)
{
Event *e = *it;
Socket *s = dynamic_cast<Socket *>(e -> GetFrom());
/*
s == NULL This is another object implementing 'IEventOwner' and not a socket.
s != NULL This is a Socket implementing IEventOwner, and we can check that the
object instance still is valid using SocketHandler::Valid.
*/
if (!s || (s && Valid( e -> Data() )))
{
e -> GetFrom() -> OnEvent(e -> GetID());
}
for (it = m_events.begin(); it != m_events.end(); ++it)
if (*it == e)
{
delete e;
break;
}
if (it != m_events.end())
m_events.erase(it);
it = m_events.begin();
}
}
long EventHandler::AddEvent(IEventOwner *from,long sec,long usec)
{
Socket *s = dynamic_cast<Socket *>(from);
Event *e = new Event(from, sec, usec, s ? s -> UniqueIdentifier() : 0);
std::list<Event *>::iterator it = m_events.begin();
while (it != m_events.end() && *(*it) < *e)
{
++it;
}
m_events.insert(it, e);
Release();
return e -> GetID();
}
void EventHandler::ClearEvents(IEventOwner *from)
{
bool repeat;
do
{
repeat = false;
for (std::list<Event *>::iterator it = m_events.begin(); it != m_events.end(); ++it)
{
Event *e = *it;
if (e -> GetFrom() == from)
{
delete e;
m_events.erase(it);
repeat = true;
break;
}
}
} while (repeat);
}
void EventHandler::EventLoop()
{
while (!m_quit)
{
struct timeval tv;
if (GetTimeUntilNextEvent(&tv))
{
Select(&tv);
CheckEvents();
}
else
{
Select();
}
}
}
void EventHandler::SetQuit(bool x)
{
m_quit = x;
}
void EventHandler::RemoveEvent(IEventOwner *from, long eid)
{
for (std::list<Event *>::iterator it = m_events.begin(); it != m_events.end(); ++it)
{
Event *e = *it;
if (from == e -> GetFrom() && eid == e -> GetID())
{
delete e;
m_events.erase(it);
break;
}
}
}
void EventHandler::Add(Socket *p)
{
SocketHandler::Add( p );
}
#ifdef SOCKETS_NAMESPACE
}
#endif
/** \file EventHandler.h
** \date 2005-12-07
** \author grymse@alhem.net
**/
/*
Copyright (C) 2005-2010 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _SOCKETS_EventHandler_H
#define _SOCKETS_EventHandler_H
#include "sockets-config.h"
#include "SocketHandler.h"
#include "IEventHandler.h"
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE
{
#endif
class StdLog;
class IEventOwner;
class Event;
class TcpSocket;
/** SocketHandler implementing the IEventHandler interface.
\ingroup timer */
class EventHandler : public SocketHandler,public IEventHandler
{
public:
EventHandler(StdLog * = NULL);
EventHandler(IMutex&,StdLog * = NULL);
~EventHandler();
bool GetTimeUntilNextEvent(struct timeval *tv);
void CheckEvents();
long AddEvent(IEventOwner *from,long sec,long usec);
void ClearEvents(IEventOwner *from);
void RemoveEvent(IEventOwner *from,long eid);
/** SocketHandler while() loop implemented with event functionality. */
void EventLoop();
/** Stop event loop. */
void SetQuit(bool = true);
void Add(Socket *);
private:
EventHandler(const EventHandler& ) {} // copy constructor
EventHandler& operator=(const EventHandler& ) {
return *this; // assignment operator
}
std::list<Event *> m_events;
bool m_quit;
};
#ifdef SOCKETS_NAMESPACE
}
#endif
#endif // _SOCKETS_EventHandler_H
/** \file EventTime.cpp
** \date 2005-12-07
** \author grymse@alhem.net
**/
/*
Copyright (C) 2005-2010 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef MACOSX
#include <stdint.h>
#include <sys/types.h>
#include <signal.h>
#endif
#include "EventTime.h"
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/select.h>
#include <sys/time.h>
#endif
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE {
#endif
EventTime::EventTime() : m_time(Tick())
{
}
EventTime::EventTime(mytime_t sec,long usec) : m_time(Tick())
{
m_time += sec * 1000000 + usec;
}
EventTime::~EventTime()
{
}
mytime_t EventTime::Tick()
{
mytime_t t;
#ifdef _WIN32
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
t = ft.dwHighDateTime;
t = t << 32;
t += ft.dwLowDateTime;
t /= 10; // us
#else
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
t = tv.tv_sec;
t *= 1000000;
t += tv.tv_usec;
#endif
return t;
}
EventTime EventTime::operator - (const EventTime& x) const
{
EventTime t;
t.m_time = m_time - x.m_time;
return t;
}
bool EventTime::operator < (const EventTime& x) const
{
return m_time < x.m_time;
}
#ifdef SOCKETS_NAMESPACE
}
#endif
/** \file EventTime.h
** \date 2005-12-07
** \author grymse@alhem.net
**/
/*
Copyright (C) 2005-2010 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _SOCKETS_EventTime_H
#define _SOCKETS_EventTime_H
#include "sockets-config.h"
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE
{
#endif
#if defined( _WIN32) && !defined(__CYGWIN__)
typedef __int64 mytime_t;
#else
#include <inttypes.h> // int64_t
typedef int64_t mytime_t;
#endif
/** \defgroup timer EventTimer event handling */
/** EventTime primitive, returns current time as a 64-bit number.
\ingroup timer */
class EventTime
{
public:
EventTime();
EventTime(mytime_t sec,long usec);
~EventTime();
static mytime_t Tick();
operator mytime_t () {
return m_time;
}
EventTime operator - (const EventTime& x) const;
bool operator < (const EventTime& x) const;
private:
EventTime(const EventTime& ) {} // copy constructor
EventTime& operator=(const EventTime& ) {
return *this; // assignment operator
}
mytime_t m_time;
};
#ifdef SOCKETS_NAMESPACE
}
#endif
#endif // _SOCKETS_EventTime_H
/**
** \file Exception.cpp
** \date 2007-09-28
** \author grymse@alhem.net
**/
/*
Copyright (C) 2007-2010 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef _MSC_VER
#pragma warning(disable:4786)
#endif
#include "Exception.h"
#include "Utility.h"
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE {
#endif
Exception::Exception(const std::string& description) : m_description(description)
, m_stack(Utility::Stack())
{
}
const std::string Exception::ToString() const
{
return m_description;
}
const std::string Exception::Stack() const
{
return m_stack;
}
#ifdef SOCKETS_NAMESPACE
} // namespace SOCKETS_NAMESPACE {
#endif
/**
** \file Exception.h
** \date 2007-09-28
** \author grymse@alhem.net
**/
/*
Copyright (C) 2007-2010 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _Sockets_Exception_H
#define _Sockets_Exception_H
#include "sockets-config.h"
#include <string>
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE
{
#endif
class Exception
{
public:
Exception(const std::string& description);
virtual ~Exception() {}
virtual const std::string ToString() const;
virtual const std::string Stack() const;
Exception(const Exception& ) {} // copy constructor
Exception& operator=(const Exception& ) {
return *this; // assignment operator
}
private:
std::string m_description;
std::string m_stack;
};
#ifdef SOCKETS_NAMESPACE
} // namespace SOCKETS_NAMESPACE {
#endif
#endif // _Sockets_Exception_H
/** \file File.cpp
** \date 2005-04-25
** \author grymse@alhem.net
**/
/*
Copyright (C) 2004-2010 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "File.h"
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE {
#endif
File::File()
:m_fil(NULL)
,m_b_close(true)
,m_rptr(0)
,m_wptr(0)
{
}
File::File(FILE *fil)
:m_fil(fil)
,m_b_close(false)
,m_rptr(0)
,m_wptr(0)
{
}
File::File(const std::string& path, const std::string& mode)
:m_fil(NULL)
,m_b_close(true)
,m_rptr(0)
,m_wptr(0)
{
fopen(path, mode);
}
File::~File()
{
if (m_b_close)
{
fclose();
}
}
bool File::fopen(const std::string& path, const std::string& mode)
{
m_path = path;
m_mode = mode;
#if defined( _WIN32) && !defined(__CYGWIN__)
if (fopen_s(&m_fil, path.c_str(), mode.c_str()))
m_fil = NULL;
#else
m_fil = ::fopen(path.c_str(), mode.c_str());
#endif
return m_fil ? true : false;
}
void File::fclose() const
{
if (m_fil)
{
::fclose(m_fil);
m_fil = NULL;
}
}
size_t File::fread(char *ptr, size_t size, size_t nmemb) const
{
size_t r = 0;
if (m_fil)
{
fseek(m_fil, m_rptr, SEEK_SET);
r = ::fread(ptr, size, nmemb, m_fil);
m_rptr = ftell(m_fil);
}
return r;
}
size_t File::fwrite(const char *ptr, size_t size, size_t nmemb)
{
size_t r = 0;
if (m_fil)
{
fseek(m_fil, m_wptr, SEEK_SET);
r = ::fwrite(ptr, size, nmemb, m_fil);
m_wptr = ftell(m_fil);
}
return r;
}
char *File::fgets(char *s, int size) const
{
char *r = NULL;
if (m_fil)
{
fseek(m_fil, m_rptr, SEEK_SET);
r = ::fgets(s, size, m_fil);
m_rptr = ftell(m_fil);
}
return r;
}
void File::fprintf(const char *format, ...)
{
if (!m_fil)
return;
va_list ap;
va_start(ap, format);
fseek(m_fil, m_wptr, SEEK_SET);
vfprintf(m_fil, format, ap);
m_wptr = ftell(m_fil);
va_end(ap);
}
off_t File::size() const
{
struct stat st;
if (stat(m_path.c_str(), &st) == -1)
{
return 0;
}
return st.st_size;
}
bool File::eof() const
{
if (m_fil)
{
if (feof(m_fil))
return true;
}
return false;
}
void File::reset_read() const
{
m_rptr = 0;
}
void File::reset_write()
{
m_wptr = 0;
}
const std::string& File::Path() const
{
return m_path;
}
#ifdef SOCKETS_NAMESPACE
}
#endif
/** \file File.h
** \date 2005-04-25
** \author grymse@alhem.net
**/
/*
Copyright (C) 2004-2010 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _SOCKETS_File_H
#define _SOCKETS_File_H
#include "sockets-config.h"
#include "IFile.h"
#include <stdio.h>
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE
{
#endif
/** IFile implementation of a disk file.
\ingroup file */
class File : public IFile
{
public:
File();
File(FILE *);
/** convenience: calls fopen() */
File(const std::string& path, const std::string& mode);
~File();
bool fopen(const std::string& path, const std::string& mode);
void fclose() const;
size_t fread(char *, size_t, size_t) const;
size_t fwrite(const char *, size_t, size_t);
char *fgets(char *, int) const;
void fprintf(const char *format, ...);
off_t size() const;
bool eof() const;
void reset_read() const;
void reset_write();
const std::string& Path() const;
private:
File(const File& ) {} // copy constructor
File& operator=(const File& ) {
return *this; // assignment operator
}
std::string m_path;
std::string m_mode;
mutable FILE *m_fil;
bool m_b_close;
mutable long m_rptr;
long m_wptr;
};
#ifdef SOCKETS_NAMESPACE
}
#endif
#endif // _SOCKETS_File_H
/**
** \file FileStream.cpp
** \date 2008-12-20
** \author grymse@alhem.net
**/
/*
Copyright (C) 2008-2010 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "FileStream.h"
#include "IFile.h"
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE {
#endif
FileStream::FileStream(IFile& file) : m_file(file)
{
}
size_t FileStream::IStreamRead(char *buf, size_t max_sz)
{
return m_file.fread(buf, 1, max_sz);
}
void FileStream::IStreamWrite(const char *buf, size_t sz)
{
m_file.fwrite(buf, 1, sz);
}
#ifdef SOCKETS_NAMESPACE
} // namespace SOCKETS_NAMESPACE {
#endif
/**
** \file FileStream.h
** \date 2008-12-20
** \author grymse@alhem.net
**/
/*
Copyright (C) 2008-2010 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _SOCKETS_FileStream_H
#define _SOCKETS_FileStream_H
#include "IStream.h"
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE
{
#endif
class IFile;
class FileStream : public IStream
{
public:
FileStream(IFile& file);
size_t IStreamRead(char *buf, size_t max_sz);
void IStreamWrite(const char *buf, size_t sz);
private:
IFile& m_file;
};
#ifdef SOCKETS_NAMESPACE
}
#endif
#endif // _SOCKETS_FileStream_H
/** \file HTTPSocket.h Class HTTPSocket definition.
** \date 2004-04-06
** \author grymse@alhem.net
**/
/*
Copyright (C) 2004-2010 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _SOCKETS_HTTPSocket_H
#define _SOCKETS_HTTPSocket_H
#include "sockets-config.h"
#include "TcpSocket.h"
#include "Utility.h"
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE
{
#endif
/** \defgroup http HTTP Sockets */
/** HTTP request/response base class.
\ingroup http */
class HTTPSocket : public TcpSocket
{
/** map to hold http header values. */
typedef Utility::ncmap<std::string> string_m;
public:
HTTPSocket(ISocketHandler& );
~HTTPSocket();
void OnRawData(const char *buf,size_t len);
void OnLine(const std::string& line);
/** Callback executes when first line has been received.
GetMethod, GetUrl/GetUri, and GetHttpVersion are valid when this callback is executed. */
virtual void OnFirst() = 0;
/** For each header line this callback is executed.
\param key Http header name
\param value Http header value */
virtual void OnHeader(const std::string& key,const std::string& value) = 0;
/** Callback fires when all http headers have been received. */
virtual void OnHeaderComplete() = 0;
/** Chunk of http body data recevied. */
virtual void OnData(const char *,size_t) = 0;
/** The full request/response body has been received. */
virtual void OnDataComplete() {}
/** Get http method from incoming request, ie GET/POST/PUT etc */
const std::string& GetMethod();
/** Set http method to be used in request. */
void SetMethod(const std::string& x);
/** Get url from request. */
const std::string& GetUrl();
/** Set url to be used in outgoing request. */
void SetUrl(const std::string& x);
/** Get part of url before '?' character. */
const std::string& GetUri();
/** Now why would I need this when there is a SetUrl method? */
void SetUri(const std::string& x);
/** Get part of url after '?' character. */
const std::string& GetQueryString();
/** Get http version from incoming request/response. */
const std::string& GetHttpVersion();
/** Get http status from incoming response. */
const std::string& GetStatus();
/** Get http statustext from incoming response. */
const std::string& GetStatusText();
/** Incoming header has been identified as a request (method url http_version\r\n). */
bool IsRequest();
/** Incoming header has been identified as a response (http_version status status_text\r\n). */
bool IsResponse();
/** Set http version to be used in outgoing request/response. */
void SetHttpVersion(const std::string& x);
/** Set http status for outgoing response. */
void SetStatus(const std::string& x);
/** Set http statustext for outgoing response. */
void SetStatusText(const std::string& x);
/** Add (and replace if exists) http header. */
void AddResponseHeader(const std::string& x,const std::string& y);
/** Add (and replace if exists) http header. */
void AddResponseHeader(const std::string& x,const char *format, ...);
/** Add http header. */
void AppendResponseHeader(const std::string& x,const std::string& y);
/** See if http header 'name' has been set. */
bool ResponseHeaderIsSet(const std::string& name);
/** Send response prepared with calls to methods SetHttpVersion, SetStatus, SetStatusText,
and AddResponseHeader. */
void SendResponse();
/** Send request prepared with calls to methods SetMethod, SetUrl, SetHttpVersion,
and AddResponseHeader. */
void SendRequest();
/** Implement this to return your own User-agent string. */
virtual std::string MyUseragent();
/** Parse url. If protocol is https, EnableSSL() will be called. */
void url_this(const std::string& url_in,std::string& protocol,std::string& host,port_t& port,std::string& url,std::string& file);
/** Transfer coding 'chunked' */
bool IsChunked() {
return m_b_chunked;
}
protected:
HTTPSocket(const HTTPSocket& s) : TcpSocket(s) {}
/** Reset state of socket to sucessfully implement keep-alive. */
virtual void Reset();
void SetMaxHeaderCount(int x) {
m_max_header_count = x;
}
private:
HTTPSocket& operator=(const HTTPSocket& ) {
return *this;
}
bool m_first;
bool m_header;
std::string m_line;
std::string m_method;
std::string m_url;
std::string m_uri;
std::string m_query_string;
std::string m_http_version;
std::string m_status;
std::string m_status_text;
bool m_request;
bool m_response;
string_m m_response_header;
size_t m_body_size_left;
bool m_b_http_1_1;
bool m_b_keepalive;
std::list<std::pair<std::string, std::string> > m_response_header_append;
bool m_b_chunked;
size_t m_chunk_size;
int m_chunk_state;
std::string m_chunk_line;
int m_header_count;
int m_max_header_count;
};
#ifdef SOCKETS_NAMESPACE
}
#endif
#endif // _SOCKETS_HTTPSocket_H
/**
** \file AjpBaseSocket.cpp
** \date 2007-10-05
** \author grymse@alhem.net
**/
/*
Copyright (C) 2007-2010 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef _MSC_VER
#pragma warning(disable:4786)
#endif
#include "HttpBaseSocket.h"
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE {
#endif
#ifdef _DEBUG
#define DEB(x) x
#else
#define DEB(x)
#endif
HttpBaseSocket::HttpBaseSocket(ISocketHandler& h)
:HTTPSocket(h)
,m_b_keepalive(false)
{
}
HttpBaseSocket::~HttpBaseSocket()
{
}
void HttpBaseSocket::OnFirst()
{
DEB(fprintf(stderr, " %s %s %s\n", GetMethod().c_str(), GetUri().c_str(), GetHttpVersion().c_str());)
m_req.SetHttpMethod( GetMethod() );
m_req.SetUri( GetUri() );
m_req.SetHttpVersion( GetHttpVersion() );
if (Utility::ToLower(GetMethod()) == "get" && !GetQueryString().empty())
{
m_req.SetAttribute("query_string", GetQueryString() );
}
m_req.SetRemoteAddr( GetRemoteAddress() );
m_req.SetRemoteHost( "" ); // %!
m_req.SetServerName( GetSockAddress() );
m_req.SetServerPort( GetSockPort() );
}
void HttpBaseSocket::OnHeader(const std::string& key,const std::string& value)
{
DEB(fprintf(stderr, " (request)OnHeader %s: %s\n", key.c_str(), value.c_str());)
if (Utility::ToLower(key) == "cookie")
m_req.AddCookie(value);
else
m_req.SetHeader(key, value);
}
void HttpBaseSocket::OnHeaderComplete()
{
m_body_size_left = atol( m_req.Header("content-length").c_str() );
if (m_body_size_left > 0)
{
m_req.InitBody( m_body_size_left );
}
else
{
// execute
Execute();
}
}
void HttpBaseSocket::OnData(const char *buf,size_t sz)
{
m_req.Write( buf, sz );
m_body_size_left -= sz;
if (!m_body_size_left)
{
m_req.CloseBody();
// execute
Execute();
}
}
// --------------------------------------------------------------------------------------
void HttpBaseSocket::Execute()
{
// parse form data / query_string and cookie header if available
m_req.ParseBody();
DEB(printf(" *** http version: %s\n", m_req.HttpVersion().c_str());
printf(" *** connection: %s\n", m_req.Header("connection").c_str());)
if ( !(m_req.HttpVersion().size() > 4 && m_req.HttpVersion().substr(m_req.HttpVersion().size() - 4) == "/1.1") ||
m_req.Header("connection") == "close")
{
m_b_keepalive = false;
DEB(printf(" *** keepalive: false\n");)
}
else
{
m_b_keepalive = true;
DEB(printf(" *** keepalive: true\n");)
}
// prepare page
OnExec( m_req );
m_req.Reset();
Reset();
}
// --------------------------------------------------------------------------------------
void HttpBaseSocket::Respond(const HttpResponse& res)
{
m_res = res;
SetHttpVersion( m_res.HttpVersion() );
SetStatus( Utility::l2string(m_res.HttpStatusCode()) );
SetStatusText( m_res.HttpStatusMsg() );
if (!ResponseHeaderIsSet("content-length"))
{
AddResponseHeader( "content-length", Utility::l2string( m_res.GetFile().size() ) );
}
for (Utility::ncmap<std::string>::const_iterator it = m_res.Headers().begin(); it != m_res.Headers().end(); ++it)
{
AddResponseHeader( it -> first, it -> second );
}
std::list<std::string> vec = m_res.CookieNames();
for (std::list<std::string>::iterator it2 = vec.begin(); it2 != vec.end(); ++it2)
{
AppendResponseHeader( "set-cookie", m_res.Cookie(*it2) );
}
SendResponse();
OnTransferLimit();
}
// --------------------------------------------------------------------------------------
void HttpBaseSocket::OnTransferLimit()
{
char msg[32768];
size_t n = m_res.GetFile().fread(msg, 1, 32768);
while (n > 0)
{
SendBuf( msg, n );
if (GetOutputLength() > 1)
{
SetTransferLimit( 1 );
break;
}
n = m_res.GetFile().fread(msg, 1, 32768);
}
if (!GetOutputLength())
{
SetTransferLimit(0);
m_res.GetFile().fclose();
OnResponseComplete();
if (!m_b_keepalive)
{
SetCloseAndDelete();
}
}
}
// --------------------------------------------------------------------------------------
void HttpBaseSocket::Reset()
{
HTTPSocket::Reset();
m_body_size_left = 0;
}
#ifdef SOCKETS_NAMESPACE
} // namespace SOCKETS_NAMESPACE {
#endif
/**
** \file HttpBaseSocket.h
** \date 2007-10-05
** \author grymse@alhem.net
**/
/*
Copyright (C) 2007-2010 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _HttpBaseSocket_H
#define _HttpBaseSocket_H
#include "HTTPSocket.h"
#include "HttpRequest.h"
#include "IHttpServer.h"
#include "HttpResponse.h"
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE
{
#endif
class HttpResponse;
class HttpBaseSocket : public HTTPSocket, public IHttpServer
{
public:
HttpBaseSocket(ISocketHandler& h);
~HttpBaseSocket();
void OnFirst();
void OnHeader(const std::string& key,const std::string& value);
void OnHeaderComplete();
void OnData(const char *,size_t);
// implements IHttpServer::Respond
void Respond(const HttpResponse& res);
void OnTransferLimit();
protected:
HttpBaseSocket(const HttpBaseSocket& s) : HTTPSocket(s) {} // copy constructor
//
HttpRequest m_req;
HttpResponse m_res;
void Reset();
private:
HttpBaseSocket& operator=(const HttpBaseSocket& ) {
return *this; // assignment operator
}
void Execute();
//
size_t m_body_size_left;
bool m_b_keepalive;
};
#ifdef SOCKETS_NAMESPACE
} // namespace SOCKETS_NAMESPACE {
#endif
#endif // _HttpBaseSocket_H
/**
** \file HttpClientSocket.cpp
** \date 2006-04-20
** \author grymse@alhem.net
**/
/*
Copyright (C) 2007-2010 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef _MSC_VER
#pragma warning(disable:4786)
#endif
#include "HttpClientSocket.h"
#include "StdLog.h"
#include "ISocketHandler.h"
#include "File.h"
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE {
#endif
HttpClientSocket::HttpClientSocket(ISocketHandler& h)
:HTTPSocket(h)
,m_data_ptr(NULL)
,m_data_size(0)
,m_content_length(0)
,m_data_ptr_set(false)
,m_fil(NULL)
,m_content_ptr(0)
,m_b_complete(false)
,m_b_close_when_complete(false)
{
}
HttpClientSocket::HttpClientSocket(ISocketHandler& h,const std::string& url_in)
:HTTPSocket(h)
,m_data_ptr(NULL)
,m_data_size(0)
,m_content_length(0)
,m_data_ptr_set(false)
,m_fil(NULL)
,m_content_ptr(0)
,m_b_complete(false)
,m_b_close_when_complete(false)
{
std::string url;
url_this(url_in, m_protocol, m_host, m_port, url, m_url_filename);
SetUrl( url );
}
HttpClientSocket::HttpClientSocket(ISocketHandler& h,const std::string& host, port_t port, const std::string& url_in)
:HTTPSocket(h)
,m_data_ptr(NULL)
,m_data_size(0)
,m_content_length(0)
,m_data_ptr_set(false)
,m_fil(NULL)
,m_content_ptr(0)
,m_b_complete(false)
,m_b_close_when_complete(false)
{
std::string url;
std::string tmp = "http://" + host + ":" + Utility::l2string(port) + url_in;
url_this(tmp, m_protocol, m_host, m_port, url, m_url_filename);
SetUrl( url );
}
HttpClientSocket::~HttpClientSocket()
{
if (m_data_ptr && !m_data_ptr_set)
{
delete[] m_data_ptr;
}
if (m_fil)
{
m_fil -> fclose();
}
}
void HttpClientSocket::OnFirst()
{
if (!IsResponse())
{
Handler().LogError(this, "OnFirst", 0, "Response expected but not received - aborting", LOG_LEVEL_FATAL);
SetCloseAndDelete();
}
m_content = GetHttpVersion() + " " + GetStatus() + " " + GetStatusText() + "\r\n";
}
void HttpClientSocket::OnHeader(const std::string& key,const std::string& value)
{
m_content += key + ": " + value + "\r\n";
if (!strcasecmp(key.c_str(), "content-length"))
{
m_content_length = atoi(value.c_str());
}
else
if (!strcasecmp(key.c_str(), "content-type"))
{
m_content_type = value;
}
}
void HttpClientSocket::OnHeaderComplete()
{
if (m_filename.size())
{
m_fil = new File;
if (!m_fil -> fopen(m_filename, "wb"))
{
delete m_fil;
m_fil = NULL;
}
}
else
if (!m_data_ptr && m_content_length)
{
m_data_ptr = new unsigned char[m_content_length];
m_data_size = m_content_length;
}
}
void HttpClientSocket::OnData(const char *buf,size_t len)
{
if (m_fil)
{
m_fil -> fwrite(buf, 1, len);
}
else
if (m_data_ptr)
{
if (m_content_ptr + len > m_data_size)
{
Handler().LogError(this, "OnData", -1, "content buffer overflow", LOG_LEVEL_ERROR);
}
else
{
memcpy(m_data_ptr + m_content_ptr, buf, len);
}
}
m_content_ptr += len;
if (m_content_ptr == m_content_length && m_content_length)
{
if (m_fil)
{
m_fil -> fclose();
delete m_fil;
m_fil = NULL;
}
m_b_complete = true;
OnContent();
if (m_b_close_when_complete)
{
SetCloseAndDelete();
}
}
}
void HttpClientSocket::OnDelete()
{
if (!m_b_complete)
{
if (m_fil)
{
m_fil -> fclose();
delete m_fil;
m_fil = NULL;
}
m_b_complete = true;
OnContent();
}
}
void HttpClientSocket::SetFilename(const std::string& x)
{
m_filename = x;
}
void HttpClientSocket::SetDataPtr(unsigned char *buf,size_t len)
{
m_data_ptr = buf;
m_data_size = len;
m_data_ptr_set = true;
}
const std::string& HttpClientSocket::GetContent()
{
return m_content;
}
size_t HttpClientSocket::GetContentLength()
{
return m_content_length;
}
size_t HttpClientSocket::GetContentPtr()
{
return m_content_ptr;
}
size_t HttpClientSocket::GetPos()
{
return m_content_ptr;
}
bool HttpClientSocket::Complete()
{
return m_b_complete;
}
const unsigned char *HttpClientSocket::GetDataPtr() const
{
return m_data_ptr;
}
void HttpClientSocket::OnContent()
{
}
void HttpClientSocket::SetCloseOnComplete(bool x)
{
m_b_close_when_complete = x;
}
const std::string& HttpClientSocket::GetUrlProtocol()
{
return m_protocol;
}
const std::string& HttpClientSocket::GetUrlHost()
{
return m_host;
}
port_t HttpClientSocket::GetUrlPort()
{
return m_port;
}
const std::string& HttpClientSocket::GetUrlFilename()
{
return m_url_filename;
}
const std::string& HttpClientSocket::GetContentType()
{
return m_content_type;
}
void HttpClientSocket::Url(const std::string& url_in,std::string& host,port_t& port)
{
std::string url;
url_this(url_in, m_protocol, m_host, m_port, url, m_url_filename);
SetUrl(url);
host = GetUrlHost();
port = GetUrlPort();
}
#ifdef SOCKETS_NAMESPACE
} // namespace SOCKETS_NAMESPACE {
#endif
/** \file HttpDebugSocket.h
** \date 2004-09-27
**/
/*
Copyright (C) 2004-2010 Anders Hedstrom (grymse@alhem.net)
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _SOCKETS_HttpDebugSocket_H
#define _SOCKETS_HttpDebugSocket_H
#include "sockets-config.h"
#include "HTTPSocket.h"
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE
{
#endif
class ISocketHandler;
/** HTTP request "echo" class. This class echoes a http request/body
with a html formatted page.
\ingroup http */
class HttpDebugSocket : public HTTPSocket
{
public:
HttpDebugSocket(ISocketHandler&);
~HttpDebugSocket();
void Init();
void OnFirst();
void OnHeader(const std::string& key,const std::string& value);
void OnHeaderComplete();
void OnData(const char *,size_t);
void OnDataComplete();
private:
HttpDebugSocket(const HttpDebugSocket& s) : HTTPSocket(s) {} // copy constructor
HttpDebugSocket& operator=(const HttpDebugSocket& ) {
return *this; // assignment operator
}
int m_content_length;
int m_read_ptr;
};
#ifdef SOCKETS_NAMESPACE
}
#endif
#endif // _SOCKETS_HttpDebugSocket_H
MAC notes
---------
The file OSX.zip contains xcode project files for an older version of the library.
Find uuid.h (if missing) here:
http://www.die.net/doc/linux/include/uuid/uuid.h
This used to be a problem, but isn't any more afaik.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment