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 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/*
* MXit Protocol libPurple Plugin
*
* -- MXit client protocol implementation --
*
* Pieter Loubser <libpurple@mxit.com>
*
* (C) Copyright 2009 MXit Lifestyle (Pty) Ltd.
* <http://www.mxitlifestyle.com>
*
* 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 02111-1301 USA
*/
#include "internal.h"
#include "purple.h"
#include "mxit.h"
#include "protocol.h"
#include "http.h"
/* HTTP constants */
#define HTTP_11_200_OK "HTTP/1.1 200 OK\r\n"
#define HTTP_11_100_CONT "HTTP/1.1 100 Continue\r\n"
#define HTTP_11_SEPERATOR "\r\n\r\n"
#define HTTP_CONTENT_LEN "Content-Length: "
/* define to enable HTTP debugging */
#define DEBUG_HTTP
/*------------------------------------------------------------------------
* This will freeup the memory used by a HTTP request structure
*
* @param req The HTTP structure's resources should be freed up
*/
static void free_http_request( struct http_request* req )
{
g_free( req->host );
g_free( req->data );
g_free( req );
}
/*------------------------------------------------------------------------
* Write the request to the HTTP server.
*
* @param fd The file descriptor
* @param pktdata The packet data
* @param pktlen The length of the packet data
* @return Return -1 on error, otherwise 0
*/
static int mxit_http_raw_write( int fd, const char* pktdata, int pktlen )
{
int written;
int res;
written = 0;
while ( written < pktlen ) {
res = write( fd, &pktdata[written], pktlen - written );
if ( res <= 0 ) {
/* error on socket */
if ( errno == EAGAIN )
continue;
purple_debug_error( MXIT_PLUGIN_ID, "Error while writing packet to HTTP server (%i)\n", res );
return -1;
}
written += res;
}
return 0;
}
/*------------------------------------------------------------------------
* Callback when data is received from the HTTP server.
*
* @param user_data The MXit session object
* @param source The file-descriptor on which data was received
* @param cond Condition which caused the callback (PURPLE_INPUT_READ)
*/
static void mxit_cb_http_read( gpointer user_data, gint source, PurpleInputCondition cond )
{
struct MXitSession* session = (struct MXitSession*) user_data;
char buf[256];
int buflen;
char* body;
int bodylen;
char* ch;
int len;
char* tmp;
int res;
char* next;
purple_debug_info( MXIT_PLUGIN_ID, "mxit_cb_http_read\n" );
if ( session->rx_state == RX_STATE_RLEN ) {
/* we are reading in the HTTP headers */
/* copy partial headers if we have any part saved */
memcpy( buf, session->rx_dbuf, session->rx_i );
buflen = session->rx_i;
/* read bytes from the socket */
len = read( session->fd, buf + buflen, sizeof( buf ) - buflen );
if ( len <= 0 ) {
/* connection has been terminated, or error occurred */
goto done;
}
//nextpacket:
#ifdef DEBUG_HTTP
purple_debug_info( MXIT_PLUGIN_ID, "HTTP POST READ 1: (%i)\n", len );
dump_bytes( session, buf + buflen, len );
#endif
/* see if we have all the HTTP headers yet */
ch = strstr( buf, HTTP_11_SEPERATOR );
if ( !ch ) {
/* we need to wait for more input, so save what we have */
session->rx_i = buflen + len;
memcpy( session->rx_dbuf, buf, session->rx_i );
return;
}
buflen += len;
/* we have the header's end now skip over the http separator to get the body offset */
ch += strlen( HTTP_11_SEPERATOR );
*(ch - 1) = '\0';
body = ch;
res = buflen - ( ch - buf );
if ( res > 0 ) {
/* we read more bytes than just the header so copy it over */
memcpy( session->rx_dbuf, ch, res );
session->rx_i = res;
}
else {
session->rx_i = 0;
}
/* test for a good response */
if ( ( strncmp( buf, HTTP_11_200_OK, strlen( HTTP_11_200_OK ) ) != 0 ) && ( strncmp( buf, HTTP_11_100_CONT, strlen( HTTP_11_100_CONT ) ) != 0 ) ) {
/* bad result */
purple_debug_error( MXIT_PLUGIN_ID, "HTTP error: %s\n", ch );
goto done;
}
/* find the content-length */
ch = (char*) purple_strcasestr( buf, HTTP_CONTENT_LEN );
if ( !ch ) {
/* bad request. it does not contain a content-length header */
purple_debug_error( MXIT_PLUGIN_ID, "HTTP reply received without content-length header (ignoring packet)\n" );
goto done;
}
/* parse the content-length */
ch += strlen( HTTP_CONTENT_LEN );
tmp = strchr( ch, '\r' );
if ( !tmp ) {
purple_debug_error( MXIT_PLUGIN_ID, "Received bad HTTP reply packet (ignoring packet)\n" );
goto done;
}
tmp = g_strndup( ch, tmp - ch );
bodylen = atoi( tmp );
g_free( tmp );
tmp = NULL;
if ( buflen > ( ( body - buf ) + bodylen ) ) {
/* we have a second packet here */
next = body + bodylen;
session->rx_res = 0;
}
else {
session->rx_res = bodylen - session->rx_i;
}
if ( session->rx_res == 0 ) {
/* we have read all the data */
session->rx_i = bodylen;
session->rx_state = RX_STATE_PROC;
}
else {
/* there is still some data outstanding */
session->rx_state = RX_STATE_DATA;
}
}
else if ( session->rx_state == RX_STATE_DATA ) {
/* we are reading the HTTP content (body) */
/* read bytes from the socket */
len = read( session->fd, &session->rx_dbuf[session->rx_i], session->rx_res );
if ( len <= 0 ) {
/* connection has been terminated, or error occurred */
goto done;
}
#ifdef DEBUG_HTTP
purple_debug_info( MXIT_PLUGIN_ID, "HTTP POST READ 2: (%i)\n", len );
dump_bytes( session, &session->rx_dbuf[session->rx_i], len );
#endif
session->rx_i += len;
session->rx_res -= len;
if ( session->rx_res == 0 ) {
/* ok, so now we have read in the whole packet */
session->rx_state = RX_STATE_PROC;
}
}
if ( session->rx_state == RX_STATE_PROC ) {
mxit_parse_packet( session );
#if 0
if ( next ) {
/* there is another packet of which we read some data */
/* reset input */
session->rx_state = RX_STATE_RLEN;
session->rx_lbuf[0] = '\0';
session->rx_i = 0;
session->rx_res = 0;
/* move read data */
len = next - buf;
buflen = len;
memcpy( buf, next, len );
goto nextpacket;
}
#endif
/* we are done */
goto done;
}
return;
done:
close( session->fd );
purple_input_remove( session->http_handler );
session->http_handler = 0;
}
/*------------------------------------------------------------------------
* Callback invoked once the connection has been established to the HTTP server,
* or on connection failure.
*
* @param user_data The MXit session object
* @param source The file-descriptor associated with the connection
* @param error_message Message explaining why the connection failed
*/
static void mxit_cb_http_connect( gpointer user_data, gint source, const gchar* error_message )
{
struct http_request* req = (struct http_request*) user_data;
purple_debug_info( MXIT_PLUGIN_ID, "mxit_cb_http_connect\n" );
/* source is the file descriptor of the new connection */
if ( source < 0 ) {
purple_debug_info( MXIT_PLUGIN_ID, "mxit_cb_http_connect failed: %s\n", error_message );
purple_connection_error( req->session->con, _( "Unable to connect to the MXit HTTP server. Please check your server settings." ) );
return;
}
/* we now have an open and active TCP connection to the mxit server */
req->session->fd = source;
/* reset the receive buffer */
req->session->rx_state = RX_STATE_RLEN;
req->session->rx_lbuf[0] = '\0';
req->session->rx_i = 0;
req->session->rx_res = 0;
/* start listening on the open connection for messages from the server (reference: "libpurple/eventloop.h") */
req->session->http_handler = purple_input_add( req->session->fd, PURPLE_INPUT_READ, mxit_cb_http_read, req->session );
/* actually send the request to the HTTP server */
mxit_http_raw_write( req->session->fd, req->data, req->datalen );
/* free up resources */
free_http_request( req );
req = NULL;
}
/*------------------------------------------------------------------------
* Create HTTP connection for sending a HTTP request
*
* @param session The MXit session object
* @param host The server name to connect to
* @param port The port number to connect to
* @param data The HTTP request data (including HTTP headers etc.)
* @param datalen The HTTP request data length
*/
void mxit_http_send_request( struct MXitSession* session, char* host, int port, const char* data, int datalen )
{
PurpleProxyConnectData* con = NULL;
struct http_request* req;
/* build the http request */
req = g_new0( struct http_request, 1 );
req->session = session;
req->host = host;
req->port = port;
req->data = g_malloc0( datalen );
memcpy( req->data, data, datalen );
req->datalen = datalen;
/* open connection to the HTTP server */
con = purple_proxy_connect( NULL, session->acc, host, port, mxit_cb_http_connect, req );
}
|