File: gopher.pike

package info (click to toggle)
roxen 1.2beta2-2
  • links: PTS
  • area: contrib
  • in suites: hamm
  • size: 16,948 kB
  • ctags: 8,589
  • sloc: ansic: 89,632; asm: 8,431; sh: 2,915; makefile: 1,787; cpp: 377
file content (358 lines) | stat: -rw-r--r-- 8,385 bytes parent folder | download | duplicates (4)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// This is a roxen module. Copyright  1996 - 1998, Idonex AB.

// Gopher proxy module.

constant cvs_version = "$Id: gopher.pike,v 1.14 1998/03/11 19:42:40 neotron Exp $";
constant thread_safe=1;

#include <config.h>

#define CONNECTION_REFUSED "HTTP/1.0 500 Connection refused by remote "	\
"host\r\nContent-type: text/html\r\n\r\n<title>Roxen: Connection "	\
"refused </title>\n<h1>Proxy request failed</h1><hr><font "		\
"size=+2><i>Connection refused by remote host</i></font><hr><font "	\
"size=-2><a href=http://www.roxen.com/>Roxen Challenger</a></font>"

#if DEBUG_LEVEL > 22
# ifndef GOPHER_DEBUG
#  define GOPHER_DEBUG
# endif
#endif

inherit "module";
inherit "socket";
inherit "roxenlib";

import Array;

#include <module.h>
#include <proxyauth.pike>

array register_module()
{
  return ({ 
    MODULE_LOCATION|MODULE_PROXY,
    "Gopher Gateway",
    "This is a caching gopher gateway, might be useful for firewall sites, if "
      "anyone is still using gopher.", ({}), 1
    });
}

string query_location() { return query("loc"); }

void create()
{
  defvar("loc", "gopher:/", "Location", TYPE_LOCATION|VAR_MORE,
	 "The mountpoint of the gopher proxy");
}


string make_html_line(string *s)
{
  if(s)
    return sprintf("<a href=\"%s\"> <img hspace=5 border=0 "
		   "src=\"%s\"> %s</a><br>\n", 
		   replace(s[1], " ", "%20"), s[0], replace(s[2], "_", " "));
  else 
    return "<p>";
}

void my_pipe_done(array (object) a)
{
//if(a[1]) destruct(a[1]);
  if(a[0]) ::my_pipe_done(a[0]);
}

void write_to_client_and_cache(object client, string data, string key)
{
  object cache;
  object pip;
  if(key)
    cache = roxen->create_cache_file("gopher", key);

  pip=Pipe.pipe();
  if(cache)
  {
    pip->set_done_callback(my_pipe_done, ({ cache, client }));
    pip->output(cache->file);
  }
  if(client->my_fd)
  {
    pip->output(client->my_fd);
    pip->write(data);
  }
}


void done_dir_data(array in)
{
  int i;
  array  dirl=(in[0]-"\r")/"\n";
  object to=in[1];
  destruct(in[2]);

  if(!objectp(to))
    return;

  if(dirl[0] && dirl[0][0] != '<')
  {
#ifdef GOPHER_DEBUG
    perror("GOPHER: Done with dir data.\n");
#endif
    dirl -= ({ ".", "" });
    for(i=0; i < sizeof(dirl); i++)
    {
      array a;
      a=dirl[i][1..]/"\t";
#define URL (a[2]+((int)a[3]==70?"":":"+a[3])+"/"+dirl[i][0..0]+a[1])
      switch(dirl[i][0])
      {
       case '0': /* File */
	dirl[i] = ({ "internal-gopher-text", "gopher://"+URL, a[0] });
	break;
	
       case '1': /* Dir  */
	dirl[i] = ({ "internal-gopher-menu", "gopher://"+URL, a[0] });
	break;
	
       case '2': /* Phonebook */
	dirl[i] = ({ "internal-gopher-index", "gopher://"+URL, a[0] });
	break;
	
       case '3': /* Error? */ 
	dirl[i] = ({ "internal-gopher-binary", "gopher://"+URL, a[0] });
	break;
	
       case '4': /* BinHex */
       case '5': /* Dos binary */
       case '6': /* Unix UUENCODED file */
       case '9': /* Binary */
	dirl[i] = ({ "internal-gopher-binary", "gopher://"+URL, a[0] });
	break;
	
       case '7': /* Search */
	dirl[i] = ({ "internal-gopher-index", "gopher://"+URL, a[0] });
	break;
	
       case '8':
	dirl[i] = ({ "internal-gopher-telnet", "telnet://"+URL, a[0] });
	break;
	
       case 'T':
	dirl[i] = ({ "internal-gopher-telnet", "tn3270://"+URL, a[0] });
	break;
	
       case '+': 
	dirl[i] = ({ "internal-gopher-menu", "gopher://"+URL, a[0] });
	break;
	
       case 'I':
       case 'g':
	dirl[i] = ({ "internal-gopher-image", "gopher://"+URL, a[0] });
	break;
	
       default:  /* Who knows.. */
	if(stringp(a[0] && strlen((string)a[0])))
	   dirl[i] = ({ "internal-gopher-unknown", "gopher://"+URL, a[0] });
	else
	  dirl[i] = 0;
      }
#undef URL
    }
#ifdef GOPHER_DEBUG
    perror("GOPHER: Sending dir data to client.\n");
#endif
    write_to_client_and_cache(to, map(dirl, make_html_line)*"" +"<hr>",
			      in[-1]);
  } else {
#ifdef GOPHER_DEBUG
    perror("GOPHER: Sending cached dir data to client.\n");
#endif
    write_to_client_and_cache(to, dirl*"\n", 0);
  }
  destruct(to);
  --roxen->num_connections;
}

void got_dir_data(array i, string s)
{
#ifdef GOPHER_DEBUG
    perror("GOPHER: Got some dir data.\n");
#endif
  i[0] += s;
  if(i[0][-1] == '.' && i[0][-2]=='\n')
    done_dir_data(i);
}

void gopher_done(object id)
{
  id->end();
}

void connected(object ok, string file, object send_to, string query, 
	       string key)
{
  string type;

#ifdef GOPHER_DEBUG
  perror("GOPHER: Connected\n");
#endif

  if(!send_to)
  {
    destruct(ok);
    return;
  }

  if(!ok)
  {
    send_to->end(CONNECTION_REFUSED);
    return;
  }

  if(strlen(file) < 2)
  {
    type="1";
    file="";
  } else {
    type=file[0..0];
    file=file[1..strlen(file)-1];
  }

#ifdef GOPHER_DEBUG
  perror("GOPHER: Requesting file\n");
#endif

  switch(type)
  {
   case "1": /* Directory, must be parsed. */
#ifdef GOPHER_DEBUG
    perror("GOPHER: Is a menu\n");
#endif
    ok->write(file + "
\n");
    ok->set_id(({ "", send_to, ok, key}));
    ok->set_nonblocking(got_dir_data, lambda(){}, done_dir_data);
    send_to->my_fd->write("HTTP/1.0 200 Yo! Gopher dir comming soon to a "
			  "screen near you\nContent-Type: text/html\n\n"
			  "<h1>Gopher menu</h1><hr>");
    return;

    /* 2 is phonebook(?). Should probably be parsed.. */
    /* 3 is error (?) */

   case "4": /* Mac binhex (of all types...) */
    ok->write(file + "
\n");
    send_to->my_fd->write("HTTP/1.0 200 Yo! Gopher data comming soon to a "
			  "screen near you\n"
			  "Content-Type: application/mac-binhex\n\n");
    break;
    
   case "5": /* Dos binary (of all types...) */
    ok->write(file + "
\n");
    send_to->my_fd->write("HTTP/1.0 200 Yo!\n"
			  "Content-Type: application/x-dosbinary\n\n");
    break;

   case "6": /* Unix UUENCODE */
    ok->write(file + "
\n");
    send_to->my_fd->write("HTTP/1.0 200 Yo!\n"
		   "Content-Type: application/x-uuencode\n\n");
    break;

   case "9": /* Binary */
    ok->write(file + "
\n");
    send_to->my_fd->write("HTTP/1.0 200 Yo!\n"
			  "Content-Type: application/binary\n\n");
    break;
    
   case "g": /* Gif image */
    ok->write(file + "
\n");
    send_to->my_fd->write("HTTP/1.0 200 Gopher data\n"
			  "Content-Type: image/gif\n\n");
    break;

   case "I": /* _some_ image, lets pretend it's a jpeg.. :) */
    ok->write(file + "
\n");
    send_to->my_fd->write("HTTP/1.0 200  to a screen near you\n"
			  "Content-Type: image/jpeg\n\n");
    break;

   case "7": /* Search, should be handled separately.. */
    if(!query)
    {
      send_to->my_fd->write("HTTP/1.0 200 Yo! Gopher data comming soon to a "
			    "screen near you\nContent-Type: text/html\n\n"
			    "<h1>Gopher search</h1>"
			    "<isindex prompt=\"Gopher search:  \">");
      destruct(ok);
      destruct(send_to);
      return;
    }
    send_to->my_fd->write("HTTP/1.0 200 Yo! Gopher data comming soon to a "
		   "screen near you\nContent-Type: text/html\n\n"
		   "<h1>Gopher Search</h1>"
		   "<isindex prompt=\"Gopher search:  \">");
    ok->write(file + "	" + query + "
\n");
    ok->set_id(({ "", send_to, ok})); 
    ok->set_nonblocking(got_dir_data, lambda(){}, done_dir_data);
    return;

   case "T": /* Tn 3270, shouldn't be here */
   case "+": /* Extra server, shouldn't be here */
   case "8": /* Shouldn't be here... */
   default:   
    ok->write(file + "
\n");
    send_to->my_fd->write("HTTP/1.0 200 Yo! Gopher data comming soon to a "
			  "screen near you\nContent-Type: text/plain\n\n");
    
  }
  async_pipe(send_to->my_fd, ok, 0, 0, "gopher", key);
  send_to->disconnect();
  /* Go for it... :) */
  return;
}

mapping find_file(string fi, object id)
{
  mixed tmp;
  string h, f, q;
  int p;
#ifdef GOPHER_DEBUG
  perror("GOPHER: find_file()\n");
#endif

  if(tmp = proxy_auth_needed(id))
    return tmp;

  sscanf(fi, "%[^/]/%s", h, f);
  if(!f)  f="";
  sscanf(h, "%s:%d", h, p);
  if(!p)  p=70;
#ifdef GOPHER_DEBUG
  perror("GOPHER: host = "+h+"\nfile = "+f+"\nport = "+p+"\n");  
#endif
  sscanf(id->raw_url, "%*s?%s", q);
  if(id->pragma["no-cache"] || id->method != "GET")
  {
    async_connect(h, p, connected, f, id, q, h+":"+p+"/"+f);
   } else {
    async_cache_connect(h, p, "gopher", h+":"+p+"/"+f,
			connected, f, id, q, h+":"+p+"/"+f);
  }
  id->do_not_disconnect = 1;  
  return http_pipe_in_progress();
}

string info()
{ 
  return "This is a simple gopher proxy, useful for firewall sites."; 
}