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
|
/*
* http.c
* Some simple routines for connecting to a remote tcp socket
* Copyright 1999 Hvard Kvlen <havardk@sol.no>
*
* 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.
*/
/* FIXME: We need to have *one* place in xmms where you configure proxies */
#include "http.h"
gint http_open_connection(gchar * server, gint port)
{
gint sock;
struct hostent *host;
struct sockaddr_in address;
sock = socket(AF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
if (!(host = gethostbyname(server)))
return 0;
memcpy(&address.sin_addr.s_addr, *(host->h_addr_list), sizeof(address.sin_addr.s_addr));
address.sin_port = g_htons(port);
if (connect(sock, (struct sockaddr *) &address, sizeof (struct sockaddr_in)) == -1)
return 0;
return sock;
}
void http_close_connection(gint sock)
{
shutdown(sock, 2);
close(sock);
}
gint http_read_line(gint sock, gchar * buf, gint size)
{
gint i = 0;
while (i < size - 1)
{
if (read(sock, buf + i, 1) <= 0)
{
if (i == 0)
return -1;
else
break;
}
if (buf[i] == '\n')
break;
if (buf[i] != '\r')
i++;
}
buf[i] = '\0';
return i;
}
gint http_read_first_line(gint sock, gchar * buf, gint size)
{
/* Skips the HTTP-header, if there is one, and reads the first line into buf.
Returns number of bytes read. */
gint i;
/* Skip the HTTP-header */
if ((i = http_read_line(sock, buf, size)) < 0)
return -1;
if (!strncmp(buf, "HTTP", 4)) /* Check to make sure its not HTTP/0.9 */
{
while (http_read_line(sock, buf, size) > 0)
/* nothing */;
if ((i = http_read_line(sock, buf, size)) < 0)
return -1;
}
return i;
}
gchar * http_get(gchar * url)
{
gchar *server, *getstr, *buf = NULL, *bptr;
gchar *gs, *gc, *turl = url;
gint sock, n, bsize, port = 0;
/* Skip past ``http://'' part of URL */
if ( !strncmp(turl, "http:", 5) ) {
turl += 5;
if ( !strncmp(turl, "//", 2) )
turl += 2;
}
/* If path starts with a '/', we are referring to localhost */
if ( turl[0] == '/' )
server = "localhost";
else
server = turl;
/* Check if URL contains port specification */
gc = strchr(turl, ':');
gs = strchr(turl, '/');
if ( gc != NULL && gc < gs ) {
port = atoi(gc+1);
*gc = '\0';
}
if ( port == 0 )
port = 80;
/* Make sure that server string is null terminated. */
if ( gs )
*gs = '\0';
/*
* Now, open connection to server.
*/
sock = http_open_connection(server, port);
/* Repair the URL string that we broke earlier on */
if ( gs )
*gs = '/';
if ( gc && gc == '\0' )
*gc = ':';
if ( sock == 0 )
return NULL;
/*
* Send query to socket.
*/
/* getstr = g_strdup_printf("GET %s HTTP/1.0\r\n\r\n", */
/* gs ? gs : "/"); */
getstr = g_strdup_printf("GET %s HTTP/1.0\r\n\r\n", url ? url : "/");
if ( write(sock, getstr, strlen(getstr)) == -1 ) {
http_close_connection(sock);
return NULL;
}
/*
* Start receiving result.
*/
bsize = 4096;
bptr = buf = g_malloc(bsize);
if ( (n = http_read_first_line(sock, bptr, bsize)) == -1 ) {
g_free(buf);
buf = NULL;
goto Done;
}
bsize -= n;
bptr += n;
while ( (n = http_read_line(sock, bptr, bsize)) != -1 ) {
bptr += n;
bsize -= n;
}
Done:
http_close_connection(sock);
/*
* Return result buffer to caller.
*/
return buf;
}
|