/[cvs]/eggdrop1.9/testcode/http.c
ViewVC logotype

Contents of /eggdrop1.9/testcode/http.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Wed Jun 19 04:51:54 2002 UTC (17 years, 9 months ago) by stdarg
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +35 -29 lines
File MIME type: text/x-chdr
* I think the sockbuf api is almost done

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <netinet/in.h>
6 #include "my_socket.h"
7 #include "sockbuf.h"
8
9 typedef struct {
10 char *host;
11 int port;
12 char *username;
13 char *password;
14 int our_idx, their_idx;
15 int sock;
16 int status;
17 } proxy_info_t;
18
19 static int http_on_read(void *client_data, int idx, char *data, int len);
20 static int http_on_eof(void *client_data, int idx, int err, const char *errmsg);
21 static int http_on_connect(void *client_data, int idx, const char *peer_ip, int peer_port);
22
23 sockbuf_handler_t http_events = {
24 "http proxy",
25 http_on_connect, http_on_eof, NULL,
26 http_on_read, NULL
27 };
28
29 static void http_free(proxy_info_t *info)
30 {
31 free(info->host);
32 free(info->username);
33 free(info->password);
34 //linemode_off(info->our_idx);
35 sockbuf_delete(info->our_idx);
36 free(info);
37 }
38
39 static void http_err(proxy_info_t *info, int err, const char *errmsg)
40 {
41 errno = err;
42 sockbuf_on_eof(info->their_idx, SOCKBUF_LEVEL_INTERNAL, err, errmsg);
43 http_free(info);
44 }
45
46 static int http_on_read(void *client_data, int idx, char *data, int len)
47 {
48 proxy_info_t *info = client_data;
49
50 if (!strncasecmp("HTTP/1.0 200", data, 12)) {
51 /* Yay, we're approved. */
52 info->status = 1;
53 return(0);
54 }
55
56 if (len != 0) {
57 /* Just some header that we don't care about. */
58 return(0);
59 }
60
61 /* It's the blank line, so the http response is done. */
62
63 if (info->status != 1) {
64 /* Boo, we weren't approved. */
65 http_err(info, ECONNREFUSED, "HTTP server refused to relay connection");
66 return(0);
67 }
68
69 /* Successful, switch it over. */
70 sockbuf_set_sock(info->our_idx, -1, 0);
71 sockbuf_set_sock(info->their_idx, info->sock, 0);
72 sockbuf_on_connect(info->their_idx, SOCKBUF_LEVEL_INTERNAL, info->host, info->port);
73 http_free(info);
74
75 return(0);
76 }
77
78 static int http_on_eof(void *client_data, int idx, int err, const char *errmsg)
79 {
80 proxy_info_t *info = client_data;
81
82 if (!err) err = ECONNREFUSED;
83 http_err(info, err, "Unexpected EOF from HTTP server");
84 return(0);
85 }
86
87 static void make_password(char *dest, proxy_info_t *info)
88 {
89 char buf[512];
90 snprintf(buf, sizeof(buf), "%s %s", info->username, info->password);
91 buf[sizeof(buf)-1] = 0;
92
93 b64enc_buf(buf, strlen(buf), dest);
94 }
95
96 static int http_on_connect(void *client_data, int idx, const char *peer_ip, int peer_port)
97 {
98 proxy_info_t *info = client_data;
99 char request[512], buf[512];
100
101 sprintf(request, "CONNECT %s:%d HTTP/1.0\r\n", info->host, info->port);
102 sprintf(buf, "Host: %s\r\n", info->host);
103 strcat(request, buf);
104 if (strlen(info->username)) {
105 char auth[512];
106
107 make_password(auth, info);
108 sprintf(buf, "Proxy-authenticate: basic %s\r\n", auth);
109 strcat(request, buf);
110 }
111 strcat(request, "\r\n");
112
113 sockbuf_write(idx, request, strlen(request));
114
115 return(0);
116 }
117
118 int http_connect(int idx, char *proxy_host, int proxy_port, char *username, char *password, char *dest_ip, int dest_port)
119 {
120 int sock;
121 proxy_info_t *info;
122
123 sock = socket_create(proxy_host, proxy_port, NULL, 0, SOCKET_CLIENT|SOCKET_TCP|SOCKET_NONBLOCK);
124 if (sock < 0) return(-1);
125
126 info = (proxy_info_t *)malloc(sizeof(*info));
127 info->host = strdup(dest_ip);
128 info->port = dest_port;
129 if (!username) username = "";
130 if (!password) password = "";
131 info->username = strdup(username);
132 info->password = strdup(password);
133
134 info->our_idx = sockbuf_new();
135 sockbuf_set_sock(info->our_idx, sock, SOCKBUF_CLIENT);
136 linemode_on(info->our_idx);
137
138 if (idx >= 0) info->their_idx = idx;
139 else info->their_idx = sockbuf_new();
140
141 info->sock = sock;
142
143 info->status = 0;
144
145 sockbuf_set_handler(info->our_idx, &http_events, info);
146
147 return(info->their_idx);
148 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23