📄 http.c
字号:
/* * http functions * * Authors: Michael Jochum <e9725005@stud3.tuwien.ac.at> * * TODO: * * Fixes: * * For license terms, see the file COPYING in the project directory. */#include <glib.h>#include <gnome.h>#include "sock.h"#include "http.h"#include "httpr.h"#define BUFLEN 1024static gint gsms_http_reconnect(GsmsHttpConnection *c);GsmsHttpConnection *gsms_http_connect(gchar *host, guint16 port, gchar *proxy, guint16 proxy_port, gint net_timeout){ GsmsHttpConnection *c; c = g_new0(GsmsHttpConnection, 1); c->host = g_strdup(host); c->port = port; c->net_timeout = net_timeout; if(proxy != NULL && (*proxy != '\0')) { host = c->proxy = proxy; port = c->proxy_port = proxy_port; c->use_proxy = TRUE; } /*Create the socket */ if ((c->sock = gsms_socket_create(net_timeout)) == NULL) { g_free(c->host); g_free(c); return NULL; } /* Open connection */ if (gsms_socket_open_connection(c->sock, host, port) < 0) { gsms_http_close(c); return NULL; } c->conalive = TRUE; return c;}static gintgsms_http_reconnect(GsmsHttpConnection *c){ gchar *host; guint16 port; if(c->conalive) /* we don't have to reconnect */ return 0; if(c->use_proxy) { host = c->proxy; port = c->proxy_port; } else { host = c->host; port = c->port; } /*Create the socket */ if ((c->sock = gsms_socket_create(c->net_timeout)) == NULL) { c->conalive = FALSE; return -1; } /* Open connection */ if (gsms_socket_open_connection(c->sock, host, port) < 0) { c->conalive = FALSE; gsms_socket_close(c->sock); return -1; } c->conalive = TRUE; return 0;}void gsms_http_close(GsmsHttpConnection *c){ if(c->conalive) gsms_socket_close(c->sock); g_free(c->host); g_free(c);}GsmsHttpResponse *gsms_http_request(GsmsHttpConnection *c, GSmsHttp *ht){ gint err, n, clen; gchar *tmp; gchar buf[BUFLEN]; gboolean keepalive = FALSE; GString *head; GsmsHttpResponse *response; if(c->use_proxy) { tmp = g_strdup_printf("http://%s%s",ht->host,ht->url); g_free(ht->url); ht->url = tmp; if(ht->connection) ht->proxy_connection = ht->connection; else ht->proxy_connection = g_strdup("Keep-Alive"); } else { if(! ht->connection) ht->connection = g_strdup("Keep-Alive"); } if ((err = gsms_http_create_message(ht)) != 0) { gsms_http_clean_up(ht); g_log(GSMS_HTTP_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Error creating http request: %s", gsms_http_strerror(err)); return NULL; } if(gsms_http_reconnect(c) < 0) { gsms_http_clean_up(ht); return NULL; } c->conalive = FALSE; /* Send request */ g_log(GSMS_HTTP_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE,"Sending request ..."); if ( (n = gsms_socket_write_block(c->sock, ht->message, strlen(ht->message)) ) < 0) { gsms_http_clean_up(ht); gsms_socket_close(c->sock); return NULL; } /* get response from server */ head = g_string_sized_new(BUFLEN); response = g_new0(GsmsHttpResponse, 1); g_log(GSMS_HTTP_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Reading response ..."); clen = -1; keepalive = FALSE; while ((n = gsms_socket_read_line(c->sock, buf, BUFLEN)) > 0) { head = g_string_append(head, buf);/* use strncmp */ if ((tmp = strstr(buf, "Content-Length:")) != NULL) { if( sscanf(tmp, "Content-Length: %d", &clen) != 1) clen = -1; } else if ((tmp = strstr(buf, "Connection: Keep-Alive")) != NULL) { keepalive = TRUE; } else if (buf[0] == 13 || buf[0] == 10) break; } if (n < 0) { gsms_http_clean_up(ht); gsms_socket_close(c->sock); g_free(response); g_string_free(head, TRUE); return NULL; } response->header = head->str; g_string_free(head, FALSE); if((tmp=strchr(response->header, ' ')) != NULL) sscanf(tmp, "%d", & response->code); if(clen < 0) { GByteArray *body = g_byte_array_new(); while((n = gsms_socket_read_block(c->sock, buf, BUFLEN)) > 0) g_byte_array_append(body, buf, n); response->document = body->data; response->content_length = body->len; g_byte_array_free(body, FALSE); } else { /* we are lucky got the content length */ response->document = g_new(gchar, clen+1); if ((n = gsms_socket_read_block(c->sock, response->document, clen)) < 0) { g_free(response->document); g_free(response->header); g_free(response); gsms_http_clean_up(ht); gsms_socket_close(c->sock); return NULL; } response->document[n] = '\0'; response->content_length = clen; } gsms_http_clean_up(ht); if(keepalive && clen > 0) c->conalive = TRUE; else gsms_socket_close(c->sock); return response;}voidgsms_http_response_free(GsmsHttpResponse *response){ g_free(response->header); g_free(response->document); g_free(response);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -