File: inet.c

package info (click to toggle)
streamripper 1.61.7-1sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,216 kB
  • ctags: 757
  • sloc: sh: 8,778; ansic: 6,138; makefile: 236
file content (149 lines) | stat: -rw-r--r-- 4,234 bytes parent folder | download
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
/* inet.c - jonclegg@yahoo.com
 * handles connecting to shoutcast streams, and getting webpages
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdlib.h>
#include <string.h>

//#include <windows.h>
#include "types.h"
#include "http.h"
#include "socklib.h"
#include "debug.h"

/*********************************************************************************
 * Private functions
 *********************************************************************************/
static error_code get_sc_header(const char* url, HSOCKET *sock, SR_HTTP_HEADER *info);

/*
 * Connects to a shoutcast type stream, leaves when it's about to get the header info
 */
error_code
inet_sc_connect(HSOCKET *sock, const char *url, const char *proxyurl, 
		SR_HTTP_HEADER *info, char *useragent, char *if_name)
{
    char headbuf[MAX_HEADER_LEN];
    URLINFO url_info;
    int ret;

    debug_printf("inet_sc_connect(): calling httplib_parse_url\n");
    if (proxyurl) {
	if ((ret = httplib_parse_url(proxyurl, &url_info)) != SR_SUCCESS)
	    return ret;
    }
    else if ((ret = httplib_parse_url(url, &url_info)) != SR_SUCCESS)
    {
	return ret;
    }

    debug_printf("inet_sc_connect(): calling sockinit\n");
    if ((ret = socklib_init()) != SR_SUCCESS)
	return ret;

    debug_printf("inet_sc_connect(): calling sock_open: host=%s, port=%d\n",
	url_info.host, url_info.port);
    if ((ret = socklib_open(sock, url_info.host, url_info.port, if_name)) != SR_SUCCESS)
	return ret;

    debug_printf("inet_sc_connect(): calling httplib_construct_sc_request\n");
    if ((ret = httplib_construct_sc_request(url, proxyurl, headbuf, useragent)) != SR_SUCCESS)
	return ret;

    debug_printf("inet_sc_connect(): calling socklib_sendall\n");
    if ((ret = socklib_sendall(sock, headbuf, strlen(headbuf))) < 0)
	return ret;

    debug_printf("inet_sc_connect(): calling get_sc_header\n");
    if ((ret = get_sc_header(url, sock, info)) != SR_SUCCESS)
	return ret;

    if (*info->http_location) {
	/* RECURSIVE CASE */
	inet_sc_connect(sock, info->http_location, proxyurl, info, useragent, if_name);
    }

    return SR_SUCCESS;
}

error_code
get_sc_header(const char* url, HSOCKET *sock, SR_HTTP_HEADER *info)
{
	int ret;
	char headbuf[MAX_HEADER_LEN] = {'\0'};

	if ((ret = socklib_read_header(sock, headbuf, MAX_HEADER_LEN, NULL)) != SR_SUCCESS)
		return ret;

	if ((ret = httplib_parse_sc_header(url, headbuf, info)) != SR_SUCCESS)
		return ret;

	return SR_SUCCESS;
}


error_code inet_get_webpage_alloc(HSOCKET *sock, const char *url, const char *proxyurl, char **buffer, unsigned long *size)
{
	char headbuf[MAX_HEADER_LEN];
	URLINFO url_info;
	int ret;

	if (proxyurl)
	{
		if ((ret = httplib_parse_url(proxyurl, &url_info)) != SR_SUCCESS)
			return ret;
	}
	else if ((ret = httplib_parse_url(url, &url_info)) != SR_SUCCESS)
	{
		return ret;
	}

	if ((ret = socklib_init()) != SR_SUCCESS)
		return ret;
		
	if ((ret = socklib_open(sock, url_info.host, url_info.port, NULL)) != SR_SUCCESS)
		return ret;
	
	if ((ret = httplib_construct_page_request(url, proxyurl != NULL, headbuf)) != SR_SUCCESS)
	{
		socklib_close(sock);
		return ret;
	}

	if ((ret = socklib_sendall(sock, headbuf, strlen(headbuf))) < 0)
	{
		socklib_close(sock);
		return ret;
	}

	memset(headbuf, 0, MAX_HEADER_LEN);
/*	if ((ret = socklib_read_header(sock, headbuf, MAX_HEADER_LEN, NULL)) != SR_SUCCESS)
	{
		socklib_close(*sock);
		return ret;
	}
*/

	if ((ret = socklib_recvall_alloc(sock, buffer, size, NULL)) != SR_SUCCESS)
	{
		socklib_close(sock);
		return ret;
	}
	socklib_close(sock);
	return SR_SUCCESS;
}