2 Syren -- a lightweight downloader for Linux/BSD/MacOSX
3 inspired by Axel Copyright 2001-2002 Wilmer van der Gaast
4 version 0.0.6 (atomic alien)
5 coded by Ketmar // Vampire Avalon
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 3 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
< 0 || ptype
> SY_PROXY_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
, const TSyPrintStr
*pfn
) {
62 const char *user
, *pass
;
65 if (!proxy
|| !host
|| !host
[0] || port
< 1 || port
> 65535) return SY_ERROR
;
66 SyMessage(pfn
, SY_MSG_NOTICE
, "CONNECTing to %s:%i", host
, port
);
67 proxy
->retCode
= -1; /* unknown yet */
68 if (SyTCPSendLine(pfn
, 1, fd
, "CONNECT %s:%i HTTP/1.1", host
, port
) != SY_OK
) return SY_ERROR
;
69 if (SyTCPSendLine(pfn
, 1, fd
, "Host: %s:%i", host
, port
) != SY_OK
) return SY_ERROR
;
70 if (proxy
->userAgent
&& *proxy
->userAgent
) {
71 if (SyTCPSendLine(pfn
, 1, fd
, "User-Agent: %s", proxy
->userAgent
) != SY_OK
) return SY_ERROR
;
73 user
= proxy
->url
->user
; pass
= proxy
->url
->pass
;
74 if ((user
&& *user
) || (pass
&& *pass
)) {
75 tmp
= SyBuildAuthStr(user
, pass
); if (!tmp
) return SY_ERROR
;
76 res
= SyTCPSendLine(pfn
, 1, fd
, "Proxy-Authorization: Basic %s", tmp
);
78 if (res
!= SY_OK
) return SY_ERROR
;
80 if (SyTCPSendLine(pfn
, 0, fd
, "") != SY_OK
) return SY_ERROR
;
83 if (!hdrs
) { SyMessage(pfn
, SY_MSG_ERROR
, "out of memory"); return SY_ERROR
; }
84 if (SyHTTPReadHeaders(hdrs
, fd
, pfn
) != SY_OK
) { SyHdrFree(hdrs
); return SY_ERROR
; }
85 proxy
->retCode
= hdrs
->code
;
86 res
= (hdrs
->code
>= 200 && hdrs
->code
<= 299)?SY_OK
:SY_ERROR
;
93 static TSyResult SySocks4Connect (TSySocket *fd, TSyProxy *proxy, const char *host, int port, const TSyPrintStr *pfn) {
94 SyMessage(pfn, SY_MSG_ERROR, "SOCKS4 proxy support is not implemented yet");
99 static TSyResult SySocks5Connect (TSySocket *fd, TSyProxy *proxy, const char *host, int port, const TSyPrintStr *pfn) {
100 SyMessage(pfn, SY_MSG_ERROR, "SOCKS5 proxy support is not implemented yet");
106 static TSyResult
SySocksConnect (TSySocket
*fd
, TSyProxy
*proxy
, const char *host
, int port
, const TSyPrintStr
*pfn
) {
107 SyMessage(pfn
, SY_MSG_ERROR
, "SOCKS proxy support is not implemented yet");
112 TSyResult
SyProxyConnect (TSySocket
*fd
, TSyProxy
*proxy
, const char *host
, int port
, const TSyPrintStr
*pfn
) {
113 if (!proxy
) return SY_ERROR
;
114 switch (proxy
->ptype
) {
115 case SY_PROXY_HTTP
: return SY_OK
;
116 case SY_PROXY_HTTP_CONNECT
: return SyHTTPConnect(fd
, proxy
, host
, port
, pfn
);
117 case SY_PROXY_SOCKS
: return SySocksConnect(fd
, proxy
, host
, port
, pfn
);
120 SyMessage(pfn
, SY_MSG_ERROR
, "unknown proxy type");