2 Syren -- a lightweight downloader for Linux/BSD/Win/MacOSX
3 inspired by Axel Copyright 2001-2002 Wilmer van der Gaast
4 version 0.0.6 (atomic alien)
5 coded by Ketmar // Avalon Group
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License with
18 the Debian GNU/Linux distribution in file /usr/doc/copyright/GPL;
19 if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 Suite 330, Boston, MA 02111-1307 USA
23 Syren proxy connections (HTTP CONNECT, SOCKS)
25 #ifndef _SYREN_PROXY_C
26 #define _SYREN_PROXY_C
28 #include "syren_proxy.h"
31 void SyProxyFree (TSyProxy
*proxy
) {
33 SyURLFree(proxy
->url
);
34 SyStrFree(proxy
->userAgent
);
39 TSyProxy
*SyProxyNew (const TSyURL
*url
, TSyProxyType ptype
, const char *userAgent
) {
42 if (!url
) return NULL
;
43 if (ptype
== SY_PROXY_NONE
) return NULL
;
44 if (ptype
< SY_PROXY_HTTP_CONNECT
|| ptype
> SY_PROXY_SOCKS_MAX
) return NULL
;
45 res
= calloc(1, sizeof(TSyProxy
));
46 if (!res
) return NULL
;
47 res
->ptype
= ptype
; res
->retCode
= -1;
48 res
->url
= SyURLClone(url
);
50 res
->userAgent
= SyStrDup(userAgent
);
51 if (res
->userAgent
) return res
;
58 /* connect with CONNECT method; uag (user agent) and retCode can be NULL */
59 static TSyResult
SyHTTPConnect (TSySocket
*fd
, TSyProxy
*proxy
, const char *host
, int port
, TSyPrintStr
*pfn
) {
63 if (!proxy
|| !host
|| !host
[0] || port
< 1 || port
> 65535) return SY_ERROR
;
64 SyMessage(pfn
, SY_MSG_NOTICE
, "CONNECTing to %s:%i", host
, port
);
65 proxy
->retCode
= -1; /* unknown yet */
66 if (SyTCPSendLine(pfn
, 1, fd
, "CONNECT %s:%i HTTP/1.0", host
, port
) != SY_OK
) return SY_ERROR
;
67 if (SyTCPSendLine(pfn
, 1, fd
, "Host: %s:%i", host
, port
) != SY_OK
) return SY_ERROR
;
68 if (proxy
->userAgent
&& *proxy
->userAgent
) {
69 if (SyTCPSendLine(pfn
, 1, fd
, "User-Agent: %s", proxy
->userAgent
) != SY_OK
) return SY_ERROR
;
71 if (SyTCPSendLine(pfn
, 0, fd
, "") != SY_OK
) return SY_ERROR
;
74 if (!hdrs
) { SyMessage(pfn
, SY_MSG_ERROR
, "out of memory"); return SY_ERROR
; }
75 if (SyHTTPReadHeaders(hdrs
, fd
, pfn
) != SY_OK
) { SyHdrFree(hdrs
); return SY_ERROR
; }
76 proxy
->retCode
= hdrs
->code
;
77 res
= (hdrs
->code
>= 200 && hdrs
->code
<= 299)?SY_OK
:SY_ERROR
;
83 static TSyResult
SySocks4Connect (TSySocket
*fd
, TSyProxy
*proxy
, const char *host
, int port
, TSyPrintStr
*pfn
) {
84 SyMessage(pfn
, SY_MSG_ERROR
, "SOCKS4 proxy support is not implemented yet");
89 static TSyResult
SySocks5Connect (TSySocket
*fd
, TSyProxy
*proxy
, const char *host
, int port
, TSyPrintStr
*pfn
) {
90 SyMessage(pfn
, SY_MSG_ERROR
, "SOCKS5 proxy support is not implemented yet");
95 TSyResult
SyProxyConnect (TSySocket
*fd
, TSyProxy
*proxy
, const char *host
, int port
, TSyPrintStr
*pfn
) {
96 if (!proxy
) return SY_ERROR
;
97 switch (proxy
->ptype
) {
98 case SY_PROXY_HTTP_CONNECT
: return SyHTTPConnect(fd
, proxy
, host
, port
, pfn
);
99 case SY_PROXY_SOCKS4
: case SY_PROXY_SOCKS4a
: return SySocks4Connect(fd
, proxy
, host
, port
, pfn
);
100 case SY_PROXY_SOCKS5
: return SySocks5Connect(fd
, proxy
, host
, port
, pfn
);
103 SyMessage(pfn
, SY_MSG_ERROR
, "unknown proxy type");