From: <the...@us...> - 2006-07-03 20:39:44
|
Revision: 16415 Author: thekingant Date: 2006-07-03 13:39:41 -0700 (Mon, 03 Jul 2006) ViewCVS: http://svn.sourceforge.net/gaim/?rev=16415&view=rev Log Message: ----------- Backport SVN revision #16414 from HEAD to v2_0_0 Original commit message: First stab at trying to fix the MSN http connect method. It still doesn't work, and I'm not sure why, but it gets a lot farther in the signon process now. For those unfamiliar with the issue, the MSN http connect method stopped working after all the non-blocking I/O changes. The http connect method is apparently used by lots of people behind silly firewalls and stuff, and therefore we really shouldn't release Gaim 2.0.0 without it working, because people will complain. The two main problems were 1. The outgoing message queue was removed in favor of buffering all data to one large buffer. This sounds good in theory... but apparently each message sent to and from the server has a "SessionID" in the HTTP header. Every message we send should use the same SessionID as the last packet we received from the server. So basically you can't put two messages into the outgoing buffer at the same time because you don't have the correct SessionID to use for the second message. You have to wait until you get the reply from the server. 2. There were some strange buffer problems with using the wrong variable when trying to combine the header+body into one buffer before sending the message. I also fixed a small memleak or two, added some comments, and tried to clean up the code a little. ViewCVS Links: ------------- http://svn.sourceforge.net/gaim/?rev=16414&view=rev Modified Paths: -------------- branches/v2_0_0/src/connection.c branches/v2_0_0/src/connection.h branches/v2_0_0/src/protocols/msn/httpconn.c branches/v2_0_0/src/protocols/msn/httpconn.h branches/v2_0_0/src/protocols/msn/msn.c Modified: branches/v2_0_0/src/connection.c =================================================================== --- branches/v2_0_0/src/connection.c 2006-07-03 20:39:04 UTC (rev 16414) +++ branches/v2_0_0/src/connection.c 2006-07-03 20:39:41 UTC (rev 16415) @@ -217,12 +217,9 @@ gaim_account_set_connection(account, NULL); - if (gc->password != NULL) - g_free(gc->password); + g_free(gc->password); + g_free(gc->display_name); - if (gc->display_name != NULL) - g_free(gc->display_name); - if (gc->disconnect_timeout) gaim_timeout_remove(gc->disconnect_timeout); Modified: branches/v2_0_0/src/connection.h =================================================================== --- branches/v2_0_0/src/connection.h 2006-07-03 20:39:04 UTC (rev 16414) +++ branches/v2_0_0/src/connection.h 2006-07-03 20:39:41 UTC (rev 16415) @@ -130,12 +130,12 @@ const char *password); /** + * Disconnects and destroys a GaimConnection. + * * This function should only be called by gaim_account_disconnect() - * in account.c. If you're trying to sign on an account, use that + * in account.c. If you're trying to sign off an account, use that * function instead. * - * Disconnects and destroys a GaimConnection. - * * @param gc The gaim connection to destroy. */ void gaim_connection_destroy(GaimConnection *gc); Modified: branches/v2_0_0/src/protocols/msn/httpconn.c =================================================================== --- branches/v2_0_0/src/protocols/msn/httpconn.c 2006-07-03 20:39:04 UTC (rev 16414) +++ branches/v2_0_0/src/protocols/msn/httpconn.c 2006-07-03 20:39:41 UTC (rev 16415) @@ -25,317 +25,242 @@ #include "debug.h" #include "httpconn.h" -static void read_cb(gpointer data, gint source, GaimInputCondition cond); -gboolean msn_httpconn_parse_data(MsnHttpConn *httpconn, const char *buf, - size_t size, char **ret_buf, size_t *ret_size, - gboolean *error); - -MsnHttpConn * -msn_httpconn_new(MsnServConn *servconn) +typedef struct { MsnHttpConn *httpconn; + char *body; + size_t body_len; +} MsnHttpQueueData; - g_return_val_if_fail(servconn != NULL, NULL); +static void +msn_httpconn_process_queue(MsnHttpConn *httpconn) +{ + httpconn->waiting_response = FALSE; - httpconn = g_new0(MsnHttpConn, 1); + if (httpconn->queue != NULL) + { + MsnHttpQueueData *queue_data; - gaim_debug_info("msn", "new httpconn (%p)\n", httpconn); + queue_data = (MsnHttpQueueData *)httpconn->queue->data; - /* TODO: Remove this */ - httpconn->session = servconn->session; + httpconn->queue = g_list_remove(httpconn->queue, queue_data); - httpconn->servconn = servconn; + msn_httpconn_write(queue_data->httpconn, + queue_data->body, + queue_data->body_len); - httpconn->tx_buf = gaim_circ_buffer_new(MSN_BUF_LEN); - httpconn->tx_handler = 0; - - return httpconn; + g_free(queue_data->body); + g_free(queue_data); + } } -void -msn_httpconn_destroy(MsnHttpConn *httpconn) +static gboolean +msn_httpconn_parse_data(MsnHttpConn *httpconn, const char *buf, + size_t size, char **ret_buf, size_t *ret_size, + gboolean *error) { - g_return_if_fail(httpconn != NULL); + const char *s, *c; + char *header, *body; + const char *body_start; + char *tmp; + size_t body_len = 0; + gboolean wasted = FALSE; - gaim_debug_info("msn", "destroy httpconn (%p)\n", httpconn); + g_return_val_if_fail(httpconn != NULL, FALSE); + g_return_val_if_fail(buf != NULL, FALSE); + g_return_val_if_fail(size > 0, FALSE); + g_return_val_if_fail(ret_buf != NULL, FALSE); + g_return_val_if_fail(ret_size != NULL, FALSE); + g_return_val_if_fail(error != NULL, FALSE); - if (httpconn->connected) - msn_httpconn_disconnect(httpconn); +#if 0 + gaim_debug_info("msn", "HTTP: parsing data {%s}\n", buf); +#endif - g_free(httpconn->full_session_id); + /* Healthy defaults. */ + body = NULL; - g_free(httpconn->session_id); + *ret_buf = NULL; + *ret_size = 0; + *error = FALSE; - g_free(httpconn->host); + /* First, some tests to see if we have a full block of stuff. */ + if (((strncmp(buf, "HTTP/1.1 200 OK\r\n", 17) != 0) && + (strncmp(buf, "HTTP/1.1 100 Continue\r\n", 23) != 0)) && + ((strncmp(buf, "HTTP/1.0 200 OK\r\n", 17) != 0) && + (strncmp(buf, "HTTP/1.0 100 Continue\r\n", 23) != 0))) + { + *error = TRUE; - gaim_circ_buffer_destroy(httpconn->tx_buf); - if (httpconn->tx_handler > 0) - gaim_input_remove(httpconn->tx_handler); - - g_free(httpconn); -} - -static char * -msn_httpconn_proxy_auth(MsnHttpConn *httpconn) -{ - GaimAccount *account; - GaimProxyInfo *gpi; - const char *username, *password; - char *auth = NULL; - - account = httpconn->session->account; - - if (gaim_account_get_proxy_info(account) == NULL) - gpi = gaim_global_proxy_get_info(); - else - gpi = gaim_account_get_proxy_info(account); - - if (gpi == NULL || !(gaim_proxy_info_get_type(gpi) == GAIM_PROXY_HTTP || - gaim_proxy_info_get_type(gpi) == GAIM_PROXY_USE_ENVVAR)) - return NULL; - - username = gaim_proxy_info_get_username(gpi); - password = gaim_proxy_info_get_password(gpi); - - if (username != NULL) { - char *tmp; - auth = g_strdup_printf("%s:%s", username, password ? password : ""); - tmp = gaim_base64_encode((const guchar *)auth, strlen(auth)); - g_free(auth); - auth = g_strdup_printf("Proxy-Authorization: Basic %s\r\n", tmp); - g_free(tmp); + return FALSE; } - return auth; -} + if (strncmp(buf, "HTTP/1.1 100 Continue\r\n", 23) == 0) + { + if ((s = strstr(buf, "\r\n\r\n")) == NULL) + return FALSE; -static void -httpconn_write_cb(gpointer data, gint source, GaimInputCondition cond) -{ - MsnHttpConn *httpconn = data; - int ret, writelen; + s += 4; - if (httpconn->waiting_response) - return; + if (*s == '\0') + { + *ret_buf = g_strdup(""); + *ret_size = 0; - writelen = gaim_circ_buffer_get_max_read(httpconn->tx_buf); + msn_httpconn_process_queue(httpconn); - if (writelen == 0) { - httpconn->waiting_response = TRUE; - gaim_input_remove(httpconn->tx_handler); - httpconn->tx_handler = 0; - return; - } + return TRUE; + } - ret = write(httpconn->fd, httpconn->tx_buf->outptr, writelen); - - if (ret < 0 && errno == EAGAIN) - return; - else if (ret <= 0) { - msn_servconn_got_error(httpconn->servconn, - MSN_SERVCONN_ERROR_WRITE); - return; + buf = s; + size -= (s - buf); } - gaim_circ_buffer_mark_read(httpconn->tx_buf, ret); + if ((s = strstr(buf, "\r\n\r\n")) == NULL) + /* Need to wait for the full HTTP header to arrive */ + return FALSE; - if (ret == writelen) - httpconn_write_cb(data, source, cond); -} + s += 4; /* Skip \r\n */ + header = g_strndup(buf, s - buf); + body_start = s; + body_len = size - (body_start - buf); -static ssize_t -write_raw(MsnHttpConn *httpconn, const char *data, size_t data_len) -{ - ssize_t res; /* result of the write operation */ - -#ifdef MSN_DEBUG_HTTP - gaim_debug_misc("msn", "Writing HTTP (header): {%s}\n", header); -#endif - - - if (httpconn->tx_handler == 0 && !httpconn->waiting_response) - res = write(httpconn->fd, data, data_len); - else + if ((s = gaim_strcasestr(header, "Content-Length: ")) != NULL) { - res = -1; - errno = EAGAIN; - } + int tmp_len; - if (res <= 0 && errno != EAGAIN) - { - msn_servconn_got_error(httpconn->servconn, - MSN_SERVCONN_ERROR_WRITE); - return -1; - } else if (res < 0 || res < data_len) { - if (res < 0) - res = 0; - if (httpconn->tx_handler == 0 && httpconn->fd) - httpconn->tx_handler = gaim_input_add(httpconn->fd, - GAIM_INPUT_WRITE, httpconn_write_cb, httpconn); - gaim_circ_buffer_append(httpconn->tx_buf, data + res, - data_len - res); - } + s += strlen("Content-Length: "); - return res; -} + if ((c = strchr(s, '\r')) == NULL) + { + g_free(header); -static void -msn_httpconn_poll(MsnHttpConn *httpconn) -{ - char *header; - char *auth; - int r; + return FALSE; + } - g_return_if_fail(httpconn != NULL); + tmp = g_strndup(s, c - s); + tmp_len = atoi(tmp); + g_free(tmp); - if (httpconn->waiting_response || - httpconn->tx_handler > 0) - { - return; - } + if (body_len != tmp_len) + { + /* Need to wait for the full packet to arrive */ - /* It is OK if this is buffered because it will only be buffered if - nothing else is in the buffer */ + g_free(header); - auth = msn_httpconn_proxy_auth(httpconn); +#if 0 + gaim_debug_warning("msn", + "body length (%d) != content length (%d)\n", + body_len, tmp_len); +#endif - header = g_strdup_printf( - "POST http://%s/gateway/gateway.dll?Action=poll&SessionID=%s HTTP/1.1\r\n" - "Accept: */*\r\n" - "Accept-Language: en-us\r\n" - "User-Agent: MSMSGS\r\n" - "Host: %s\r\n" - "Proxy-Connection: Keep-Alive\r\n" - "%s" /* Proxy auth */ - "Connection: Keep-Alive\r\n" - "Pragma: no-cache\r\n" - "Content-Type: application/x-msn-messenger\r\n" - "Content-Length: 0\r\n\r\n", - httpconn->host, - httpconn->full_session_id, - httpconn->host, - auth ? auth : ""); - - g_free(auth); - - r = write_raw(httpconn, header, strlen(header)); - - g_free(header); - - if (r >= 0) - { - httpconn->waiting_response = TRUE; - httpconn->dirty = FALSE; + return FALSE; + } } -} -static gboolean -do_poll(gpointer data) -{ - MsnHttpConn *httpconn; + body = g_malloc0(body_len + 1); + memcpy(body, body_start, body_len); - httpconn = data; - - g_return_val_if_fail(httpconn != NULL, TRUE); - -#if 0 - gaim_debug_info("msn", "polling from %s\n", httpconn->session_id); +#ifdef MSN_DEBUG_HTTP + gaim_debug_misc("msn", "Incoming HTTP buffer (header): {%s\r\n}\n", + header); #endif - if ((httpconn->host == NULL) || (httpconn->full_session_id == NULL)) + /* Now we should be able to process the data. */ + if ((s = gaim_strcasestr(header, "X-MSN-Messenger: ")) != NULL) { - gaim_debug_warning("msn", "Attempted HTTP poll before session is established\n"); - return TRUE; - } + char *full_session_id, *gw_ip, *session_action; + char *t, *session_id; + char **elems, **cur, **tokens; - if (httpconn->dirty) - msn_httpconn_poll(httpconn); + full_session_id = gw_ip = session_action = NULL; - return TRUE; -} + s += strlen("X-MSN-Messenger: "); -static void -connect_cb(gpointer data, gint source, GaimInputCondition cond) -{ - MsnHttpConn *httpconn = data; + if ((c = strchr(s, '\r')) == NULL) + { + msn_session_set_error(httpconn->session, + MSN_ERROR_HTTP_MALFORMED, NULL); + gaim_debug_error("msn", "Malformed X-MSN-Messenger field.\n{%s}", + buf); - httpconn->fd = source; + g_free(body); + return FALSE; + } - if (source > 0) - { - httpconn->inpa = gaim_input_add(httpconn->fd, GAIM_INPUT_READ, - read_cb, data); + tmp = g_strndup(s, c - s); - httpconn->timer = gaim_timeout_add(2000, do_poll, httpconn); + elems = g_strsplit(tmp, "; ", 0); - httpconn->waiting_response = FALSE; - if (httpconn->tx_handler > 0) - gaim_input_remove(httpconn->tx_handler); + for (cur = elems; *cur != NULL; cur++) + { + tokens = g_strsplit(*cur, "=", 2); - httpconn->tx_handler = gaim_input_add(source, - GAIM_INPUT_WRITE, httpconn_write_cb, httpconn); + if (strcmp(tokens[0], "SessionID") == 0) + full_session_id = tokens[1]; + else if (strcmp(tokens[0], "GW-IP") == 0) + gw_ip = tokens[1]; + else if (strcmp(tokens[0], "Session") == 0) + session_action = tokens[1]; + else + g_free(tokens[1]); - httpconn_write_cb(httpconn, source, GAIM_INPUT_WRITE); - } - else - { - gaim_debug_error("msn", "HTTP: Connection error\n"); - msn_servconn_got_error(httpconn->servconn, MSN_SERVCONN_ERROR_CONNECT); - } -} + g_free(tokens[0]); + /* Don't free each of the tokens, only the array. */ + g_free(tokens); + } -gboolean -msn_httpconn_connect(MsnHttpConn *httpconn, const char *host, int port) -{ - int r; + g_strfreev(elems); - g_return_val_if_fail(httpconn != NULL, FALSE); - g_return_val_if_fail(host != NULL, FALSE); - g_return_val_if_fail(port > 0, FALSE); + g_free(tmp); - if (httpconn->connected) - msn_httpconn_disconnect(httpconn); + if ((session_action != NULL) && (strcmp(session_action, "close") == 0)) + wasted = TRUE; - r = gaim_proxy_connect(httpconn->session->account, - "gateway.messenger.hotmail.com", 80, connect_cb, httpconn); + g_free(session_action); - if (r == 0) - { - httpconn->waiting_response = TRUE; - httpconn->connected = TRUE; - } + t = strchr(full_session_id, '.'); + session_id = g_strndup(full_session_id, t - full_session_id); - return httpconn->connected; -} + if (!wasted) + { + g_free(httpconn->full_session_id); + httpconn->full_session_id = full_session_id; -void -msn_httpconn_disconnect(MsnHttpConn *httpconn) -{ - g_return_if_fail(httpconn != NULL); + g_free(httpconn->session_id); + httpconn->session_id = session_id; - if (!httpconn->connected) - return; + g_free(httpconn->host); + httpconn->host = gw_ip; + } + else + { + MsnServConn *servconn; - if (httpconn->timer) - gaim_timeout_remove(httpconn->timer); + /* It's going to die. */ + /* poor thing */ - httpconn->timer = 0; + servconn = httpconn->servconn; - if (httpconn->inpa > 0) - { - gaim_input_remove(httpconn->inpa); - httpconn->inpa = 0; + /* I'll be honest, I don't fully understand all this, but this + * causes crashes, Stu. */ + /* if (servconn != NULL) + servconn->wasted = TRUE; */ + + g_free(full_session_id); + g_free(session_id); + g_free(gw_ip); + } } - close(httpconn->fd); + g_free(header); - g_free(httpconn->rx_buf); - httpconn->rx_buf = NULL; - httpconn->rx_len = 0; + *ret_buf = body; + *ret_size = body_len; - httpconn->connected = FALSE; + msn_httpconn_process_queue(httpconn); - /* msn_servconn_disconnect(httpconn->servconn); */ + return TRUE; } static void @@ -376,7 +301,7 @@ if (!msn_httpconn_parse_data(httpconn, httpconn->rx_buf, httpconn->rx_len, &result_msg, &result_len, &error)) { - /* We must wait for more input, or something went wrong */ + /* Either we must wait for more input, or something went wrong */ if (error) msn_servconn_got_error(httpconn->servconn, MSN_SERVCONN_ERROR_READ); @@ -472,15 +397,169 @@ g_free(old_rx_buf); } +static void +httpconn_write_cb(gpointer data, gint source, GaimInputCondition cond) +{ + MsnHttpConn *httpconn; + int ret, writelen; + + httpconn = data; + writelen = gaim_circ_buffer_get_max_read(httpconn->tx_buf); + + if (writelen == 0) + { + gaim_input_remove(httpconn->tx_handler); + httpconn->tx_handler = 0; + return; + } + + ret = write(httpconn->fd, httpconn->tx_buf->outptr, writelen); + if (ret <= 0) + { + if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) + /* No worries */ + return; + + /* Error! */ + msn_servconn_got_error(httpconn->servconn, MSN_SERVCONN_ERROR_WRITE); + return; + } + + gaim_circ_buffer_mark_read(httpconn->tx_buf, ret); + + /* TODO: I don't think these 2 lines are needed. Remove them? */ + if (ret == writelen) + httpconn_write_cb(data, source, cond); +} + +static gboolean +write_raw(MsnHttpConn *httpconn, const char *data, size_t data_len) +{ + ssize_t res; /* result of the write operation */ + + if (httpconn->tx_handler == 0) + res = write(httpconn->fd, data, data_len); + else + { + res = -1; + errno = EAGAIN; + } + + if ((res <= 0) && ((errno != EAGAIN) && (errno != EWOULDBLOCK))) + { + msn_servconn_got_error(httpconn->servconn, MSN_SERVCONN_ERROR_WRITE); + return FALSE; + } + + if (res < 0 || res < data_len) + { + if (res < 0) + res = 0; + if (httpconn->tx_handler == 0 && httpconn->fd) + httpconn->tx_handler = gaim_input_add(httpconn->fd, + GAIM_INPUT_WRITE, httpconn_write_cb, httpconn); + gaim_circ_buffer_append(httpconn->tx_buf, data + res, + data_len - res); + } + + return TRUE; +} + +static char * +msn_httpconn_proxy_auth(MsnHttpConn *httpconn) +{ + GaimAccount *account; + GaimProxyInfo *gpi; + const char *username, *password; + char *auth = NULL; + + account = httpconn->session->account; + + if (gaim_account_get_proxy_info(account) == NULL) + gpi = gaim_global_proxy_get_info(); + else + gpi = gaim_account_get_proxy_info(account); + + if (gpi == NULL || !(gaim_proxy_info_get_type(gpi) == GAIM_PROXY_HTTP || + gaim_proxy_info_get_type(gpi) == GAIM_PROXY_USE_ENVVAR)) + return NULL; + + username = gaim_proxy_info_get_username(gpi); + password = gaim_proxy_info_get_password(gpi); + + if (username != NULL) { + char *tmp; + auth = g_strdup_printf("%s:%s", username, password ? password : ""); + tmp = gaim_base64_encode((const guchar *)auth, strlen(auth)); + g_free(auth); + auth = g_strdup_printf("Proxy-Authorization: Basic %s\r\n", tmp); + g_free(tmp); + } + + return auth; +} + +static gboolean +msn_httpconn_poll(gpointer data) +{ + MsnHttpConn *httpconn; + char *header; + char *auth; + + data = httpconn; + + g_return_val_if_fail(httpconn != NULL, FALSE); + + if ((httpconn->host == NULL) || (httpconn->full_session_id == NULL)) + { + /* There's no need to poll if the session is not fully established */ + return TRUE; + } + + if (httpconn->waiting_response) + { + /* There's no need to poll if we're already waiting for a response */ + return TRUE; + } + + auth = msn_httpconn_proxy_auth(httpconn); + + header = g_strdup_printf( + "POST http://%s/gateway/gateway.dll?Action=poll&SessionID=%s HTTP/1.1\r\n" + "Accept: */*\r\n" + "Accept-Language: en-us\r\n" + "User-Agent: MSMSGS\r\n" + "Host: %s\r\n" + "Proxy-Connection: Keep-Alive\r\n" + "%s" /* Proxy auth */ + "Connection: Keep-Alive\r\n" + "Pragma: no-cache\r\n" + "Content-Type: application/x-msn-messenger\r\n" + "Content-Length: 0\r\n\r\n", + httpconn->host, + httpconn->full_session_id, + httpconn->host, + auth ? auth : ""); + + g_free(auth); + + if (write_raw(httpconn, header, strlen(header))) + httpconn->waiting_response = TRUE; + + g_free(header); + + return TRUE; +} + ssize_t -msn_httpconn_write(MsnHttpConn *httpconn, const char *body, size_t size) +msn_httpconn_write(MsnHttpConn *httpconn, const char *body, size_t body_len) { char *params; char *data; + int header_len; char *auth; const char *server_types[] = { "NS", "SB" }; const char *server_type; - ssize_t r; /* result of the write operation */ char *host; MsnServConn *servconn; @@ -488,10 +567,23 @@ g_return_val_if_fail(httpconn != NULL, 0); g_return_val_if_fail(body != NULL, 0); - g_return_val_if_fail(size > 0, 0); + g_return_val_if_fail(body_len > 0, 0); servconn = httpconn->servconn; + if (httpconn->waiting_response) + { + MsnHttpQueueData *queue_data = g_new0(MsnHttpQueueData, 1); + + queue_data->httpconn = httpconn; + queue_data->body = g_memdup(body, body_len); + queue_data->body_len = body_len; + + httpconn->queue = g_list_append(httpconn->queue, queue_data); + + return body_len; + } + server_type = server_types[servconn->type]; if (httpconn->virgin) @@ -532,245 +624,158 @@ "Connection: Keep-Alive\r\n" "Pragma: no-cache\r\n" "Content-Type: application/x-msn-messenger\r\n" - "Content-Length: %d\r\n\r\n" - "%s", + "Content-Length: %d\r\n\r\n", host, params, host, auth ? auth : "", - (int) size, - body ? body : ""); + (int) body_len); - g_free(params); g_free(auth); - r = write_raw(httpconn, data, strlen(data)); + header_len = strlen(data); + data = g_realloc(data, header_len + body_len); + memcpy(data + header_len, body, body_len); + if (write_raw(httpconn, data, header_len + body_len)) + httpconn->waiting_response = TRUE; + g_free(data); - if (r >= 0) - { - httpconn->waiting_response = TRUE; - httpconn->dirty = FALSE; - } - - return r; + return body_len; } -gboolean -msn_httpconn_parse_data(MsnHttpConn *httpconn, const char *buf, - size_t size, char **ret_buf, size_t *ret_size, - gboolean *error) +MsnHttpConn * +msn_httpconn_new(MsnServConn *servconn) { - const char *s, *c; - char *header, *body; - const char *body_start; - char *tmp; - size_t body_len = 0; - gboolean wasted = FALSE; + MsnHttpConn *httpconn; - g_return_val_if_fail(httpconn != NULL, FALSE); - g_return_val_if_fail(buf != NULL, FALSE); - g_return_val_if_fail(size > 0, FALSE); - g_return_val_if_fail(ret_buf != NULL, FALSE); - g_return_val_if_fail(ret_size != NULL, FALSE); - g_return_val_if_fail(error != NULL, FALSE); + g_return_val_if_fail(servconn != NULL, NULL); -#if 0 - gaim_debug_info("msn", "HTTP: parsing data {%s}\n", buf); -#endif + httpconn = g_new0(MsnHttpConn, 1); - httpconn->waiting_response = FALSE; + gaim_debug_info("msn", "new httpconn (%p)\n", httpconn); - /* Healthy defaults. */ - body = NULL; + /* TODO: Remove this */ + httpconn->session = servconn->session; - *ret_buf = NULL; - *ret_size = 0; - *error = FALSE; + httpconn->servconn = servconn; - /* First, some tests to see if we have a full block of stuff. */ - if (((strncmp(buf, "HTTP/1.1 200 OK\r\n", 17) != 0) && - (strncmp(buf, "HTTP/1.1 100 Continue\r\n", 23) != 0)) && - ((strncmp(buf, "HTTP/1.0 200 OK\r\n", 17) != 0) && - (strncmp(buf, "HTTP/1.0 100 Continue\r\n", 23) != 0))) - { - *error = TRUE; + httpconn->tx_buf = gaim_circ_buffer_new(MSN_BUF_LEN); + httpconn->tx_handler = 0; - return FALSE; - } + return httpconn; +} - if (strncmp(buf, "HTTP/1.1 100 Continue\r\n", 23) == 0) - { - if ((s = strstr(buf, "\r\n\r\n")) == NULL) - return FALSE; +void +msn_httpconn_destroy(MsnHttpConn *httpconn) +{ + g_return_if_fail(httpconn != NULL); - s += 4; + gaim_debug_info("msn", "destroy httpconn (%p)\n", httpconn); - if (*s == '\0') - { - *ret_buf = g_strdup(""); - *ret_size = 0; + if (httpconn->connected) + msn_httpconn_disconnect(httpconn); - if (httpconn->tx_handler > 0) - httpconn_write_cb(httpconn, httpconn->fd, - GAIM_INPUT_WRITE); - else - httpconn->dirty = TRUE; + g_free(httpconn->full_session_id); - return TRUE; - } + g_free(httpconn->session_id); - buf = s; - size -= (s - buf); - } + g_free(httpconn->host); - if ((s = strstr(buf, "\r\n\r\n")) == NULL) - return FALSE; + gaim_circ_buffer_destroy(httpconn->tx_buf); + if (httpconn->tx_handler > 0) + gaim_input_remove(httpconn->tx_handler); - s += 4; /* Skip \r\n */ - header = g_strndup(buf, s - buf); - body_start = s; - body_len = size - (body_start - buf); + g_free(httpconn); +} - if ((s = gaim_strcasestr(header, "Content-Length: ")) != NULL) +static void +connect_cb(gpointer data, gint source, GaimInputCondition cond) +{ + MsnHttpConn *httpconn = data; + + /* + TODO: Need to do this in case the account is disabled while connecting + if (!g_list_find(gaim_connections_get_all(), gc)) { - int tmp_len; + if (source >= 0) + close(source); + destroy_new_conn_data(new_conn_data); + return; + } + */ - s += strlen("Content-Length: "); + httpconn->fd = source; - if ((c = strchr(s, '\r')) == NULL) - { - g_free(header); + if (source >= 0) + { + httpconn->inpa = gaim_input_add(httpconn->fd, GAIM_INPUT_READ, + read_cb, data); - return FALSE; - } + httpconn->timer = gaim_timeout_add(2000, msn_httpconn_poll, httpconn); - tmp = g_strndup(s, c - s); - tmp_len = atoi(tmp); - g_free(tmp); - - if (body_len != tmp_len) - { - g_free(header); - -#if 0 - gaim_debug_warning("msn", - "body length (%d) != content length (%d)\n", - body_len, tmp_len); -#endif - - return FALSE; - } + msn_httpconn_process_queue(httpconn); } - - body = g_malloc0(body_len + 1); - memcpy(body, body_start, body_len); - -#ifdef MSN_DEBUG_HTTP - gaim_debug_misc("msn", "Incoming HTTP buffer (header): {%s\r\n}\n", - header); -#endif - - /* Now we should be able to process the data. */ - if ((s = gaim_strcasestr(header, "X-MSN-Messenger: ")) != NULL) + else { - char *full_session_id, *gw_ip, *session_action; - char *t, *session_id; - char **elems, **cur, **tokens; + gaim_debug_error("msn", "HTTP: Connection error\n"); + msn_servconn_got_error(httpconn->servconn, MSN_SERVCONN_ERROR_CONNECT); + } +} - full_session_id = gw_ip = session_action = NULL; +gboolean +msn_httpconn_connect(MsnHttpConn *httpconn, const char *host, int port) +{ + int r; - s += strlen("X-MSN-Messenger: "); + g_return_val_if_fail(httpconn != NULL, FALSE); + g_return_val_if_fail(host != NULL, FALSE); + g_return_val_if_fail(port > 0, FALSE); - if ((c = strchr(s, '\r')) == NULL) - { - msn_session_set_error(httpconn->session, - MSN_ERROR_HTTP_MALFORMED, NULL); - gaim_debug_error("msn", "Malformed X-MSN-Messenger field.\n{%s}", - buf); + if (httpconn->connected) + msn_httpconn_disconnect(httpconn); - g_free(body); - return FALSE; - } + r = gaim_proxy_connect(httpconn->session->account, + "gateway.messenger.hotmail.com", 80, connect_cb, httpconn); - tmp = g_strndup(s, c - s); + if (r == 0) + { + httpconn->waiting_response = TRUE; + httpconn->connected = TRUE; + } - elems = g_strsplit(tmp, "; ", 0); + return httpconn->connected; +} - for (cur = elems; *cur != NULL; cur++) - { - tokens = g_strsplit(*cur, "=", 2); +void +msn_httpconn_disconnect(MsnHttpConn *httpconn) +{ + g_return_if_fail(httpconn != NULL); - if (strcmp(tokens[0], "SessionID") == 0) - full_session_id = tokens[1]; - else if (strcmp(tokens[0], "GW-IP") == 0) - gw_ip = tokens[1]; - else if (strcmp(tokens[0], "Session") == 0) - session_action = tokens[1]; + if (!httpconn->connected) + return; - g_free(tokens[0]); - /* Don't free each of the tokens, only the array. */ - g_free(tokens); - } + if (httpconn->timer) + gaim_timeout_remove(httpconn->timer); - g_strfreev(elems); + httpconn->timer = 0; - g_free(tmp); - - if ((session_action != NULL) && (strcmp(session_action, "close") == 0)) - wasted = TRUE; - - g_free(session_action); - - t = strchr(full_session_id, '.'); - session_id = g_strndup(full_session_id, t - full_session_id); - - if (!wasted) - { - g_free(httpconn->full_session_id); - - httpconn->full_session_id = full_session_id; - - g_free(httpconn->session_id); - - httpconn->session_id = session_id; - - g_free(httpconn->host); - - httpconn->host = gw_ip; - } - else - { - MsnServConn *servconn; - - /* It's going to die. */ - /* poor thing */ - - servconn = httpconn->servconn; - - /* I'll be honest, I don't fully understand all this, but this - * causes crashes, Stu. */ - /* if (servconn != NULL) - servconn->wasted = TRUE; */ - - g_free(full_session_id); - g_free(session_id); - g_free(gw_ip); - } + if (httpconn->inpa > 0) + { + gaim_input_remove(httpconn->inpa); + httpconn->inpa = 0; } - g_free(header); + close(httpconn->fd); - *ret_buf = body; - *ret_size = body_len; + g_free(httpconn->rx_buf); + httpconn->rx_buf = NULL; + httpconn->rx_len = 0; - if (httpconn->tx_handler > 0) - httpconn_write_cb(httpconn, httpconn->fd, GAIM_INPUT_WRITE); - else - httpconn->dirty = TRUE; + httpconn->connected = FALSE; - return TRUE; + /* msn_servconn_disconnect(httpconn->servconn); */ } Modified: branches/v2_0_0/src/protocols/msn/httpconn.h =================================================================== --- branches/v2_0_0/src/protocols/msn/httpconn.h 2006-07-03 20:39:04 UTC (rev 16414) +++ branches/v2_0_0/src/protocols/msn/httpconn.h 2006-07-03 20:39:41 UTC (rev 16415) @@ -44,13 +44,13 @@ gboolean waiting_response; /**< The flag that states if we are waiting a response from the server. */ - gboolean dirty; /**< The flag that states if we should poll. */ gboolean connected; /**< The flag that states if the connection is on. */ gboolean virgin; /**< The flag that states if this connection should specify the host (not gateway) to connect to. */ char *host; /**< The HTTP gateway host. */ + GList *queue; /**< The queue of data chunks to write. */ int fd; /**< The connection's file descriptor. */ guint inpa; /**< The connection's input handler. */ @@ -83,11 +83,11 @@ * * @param servconn The server connection. * @param data The data to write. - * @param size The size of the data to write. + * @param data_len The size of the data to write. * * @return The number of bytes written. */ -ssize_t msn_httpconn_write(MsnHttpConn *httpconn, const char *data, size_t size); +ssize_t msn_httpconn_write(MsnHttpConn *httpconn, const char *data, size_t data_len); /** * Connects the HTTP connection object to a host. Modified: branches/v2_0_0/src/protocols/msn/msn.c =================================================================== --- branches/v2_0_0/src/protocols/msn/msn.c 2006-07-03 20:39:04 UTC (rev 16414) +++ branches/v2_0_0/src/protocols/msn/msn.c 2006-07-03 20:39:41 UTC (rev 16415) @@ -731,8 +731,7 @@ return; } - if (gaim_account_get_bool(account, "http_method", FALSE)) - http_method = TRUE; + http_method = gaim_account_get_bool(account, "http_method", FALSE); host = gaim_account_get_string(account, "server", MSN_SERVER); port = gaim_account_get_int(account, "port", MSN_PORT); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |