History

20070312c webgrab

Charles.Forsyth committed 19 years ago

Commit e2187311 parent 50734c83 files · +52 −7Browse files

Unified · Split

3 files
  1. appl/cmd/webgrab.b +42 −8
  2. dis/webgrab.dis +0 −0
  3. man/1/webgrab +11 −0
appl/cmd/webgrab.b+42−8
@@ -40,6 +40,7 @@ Webgrab: module · 39 unmodified lines
4040
4141 stderr: ref FD;
4242 verbose := 0;
43+postbody : string;
4344
4445 httpproxy: ref Url->ParsedUrl;
4546 noproxydoms: list of string; # domains that don't require proxy
@@ -59,7 +60,7 @@ init(nil: ref Draw->Context, args: list of string) · 13 unmodified lines
5960 stem := "";
6061 rawflag := 0;
6162 arg->init(args);
62− arg->setusage("webgrab [-r] [-v[v]] [-o stem] url");
63+ arg->setusage("webgrab [-r] [-v[v]] [-p postbody] [-o stem] url");
6364 url := "";
6465 while((o := arg->opt()) != 0)
6566 case o {
@@ -69,6 +70,8 @@ init(nil: ref Draw->Context, args: list of string) · 3 unmodified lines
6970 verbose++;
7071 'o' =>
7172 stem = arg->earg();
73+ 'p' =>
74+ postbody = arg->earg();
7275 * =>
7376 arg->usage();
7477 }
@@ -174,7 +177,7 @@ grab(u: ref ParsedUrl, stem: string, rawflag: int) · 99 unmodified lines
174177 (fname, suburl) := hd l;
175178 subu := U->makeurl(suburl);
176179 subu.makeabsolute(actual);
177− (suberr, subcontents, subfd, subactual) := httpget(subu);
180+ (suberr, subcontents, subfd, nil) := httpget(subu);
178181 if(suberr != "") {
179182 sys->fprint(stderr, "webgrab: can't fetch subordinate %s from %s: %s\n", fname, subu.tostring(), suberr);
180183 continue;
@@ -361,17 +364,18 @@ httpget(u: ref ParsedUrl) : (string, array of byte, ref Sys->FD, ref ParsedUrl) · 180 unmodified lines
361364 {
362365 ans, body : array of byte;
363366 restfd: ref Sys->FD;
367+ req : string;
368+
364369 for(redir := 0; redir < 10; redir++) {
365370 if(u.port == "")
366371 u.port = "80"; # default IP port for HTTP
367372 if(verbose)
368373 sys->fprint(stderr, "connecting to %s\n", u.host);
369374 dialhost, port: string;
370− req := "GET ";
375+
371376 if(httpproxy != nil && need_proxy(u.host)) {
372377 dialhost = httpproxy.host;
373378 port = httpproxy.port;
374− req += "http://" + u.host;
375379 }
376380 else {
377381 dialhost = u.host;
@@ -380,10 +384,39 @@ httpget(u: ref ParsedUrl) : (string, array of byte, ref Sys->FD, ref ParsedUrl) · 2 unmodified lines
380384 (ok, net) := sys->dial("tcp!" + dialhost + "!" + port, nil);
381385 if(ok < 0)
382386 return (sys->sprint("can't dial %s: %r", dialhost), nil, nil, nil);
383− req += "/" + u.path;
384− if(u.query != "")
385− req += "?" + u.query;
386− req += " HTTP/1.0\r\nHost: "+u.host+"\r\nUser-agent: Inferno/webgrab\r\n\r\n";
387+
388+ # prepare request
389+ if(u.query != ""){
390+ u.query = "?" + u.query;
391+ }
392+
393+ if (postbody == nil){
394+ if(httpproxy == nil || !need_proxy(u.host)){
395+ req = sys->sprint("GET /%s%s HTTP/1.0\r\n"+
396+ "Host: %s\r\n"+
397+ "User-agent: Inferno/webgrab\r\n"+
398+ "Cache-Control: no-cache\r\n"+
399+ "Pragma: no-cache\r\n\r\n",
400+ u.path, u.query, u.host);
401+ }else{
402+ req = sys->sprint("GET http:///%s%s HTTP/1.0\r\n"+
403+ "Host: %s\r\n"+
404+ "User-agent: Inferno/webgrab\r\n"+
405+ "Cache-Control: no-cache\r\n"+
406+ "Pragma: no-cache\r\n\r\n",
407+ u.host, u.path, u.host);
408+ }
409+ }else{
410+ req = sys->sprint("POST /%s HTTP/1.0\r\n"+
411+ "Host: %s\r\n"+
412+ "Content-type: application/x-www-form-urlencoded\r\n"+
413+ "Content-length: %d\r\n"+
414+ "User-agent: Inferno/webgrab\r\n"+
415+ "\r\n"+"%s",
416+ u.path, u.host, len postbody, postbody);
417+
418+ }
419+
387420 if(verbose)
388421 sys->fprint(stderr, "writing request: %s\n", req);
389422 areq := array of byte req;
@@ -433,6 +466,7 @@ httpget(u: ref ParsedUrl) : (string, array of byte, ref Sys->FD, ref ParsedUrl) · 43 unmodified lines
433466 return ("", body, restfd, u);
434467 }
435468
469+
436470 need_proxy(h: string) : int
437471 {
438472 doml := noproxydoms;
dis/webgrab.dis+0−0

Binary file not shown.

man/1/webgrab+11−0
@@ -9,6 +9,8 @@ webgrab \- fetch web page content as files · 8 unmodified lines
99 .B -v
1010 ] [
1111 .BI -o " stem"
12+] [
13+.BI -p " body"
1214 ]
1315 .I url
1416 .SH DESCRIPTION
@@ -93,6 +95,15 @@ print a chatty progress report · 78 unmodified lines
9395 use the
9496 .I stem
9597 as given
98+.TP
99+.BI -p " body"
100+Use HTTP
101+.B POST
102+instead of
103+.BR GET ,
104+posting
105+.I body
106+as the data
96107 .PP
97108 .I Webgrab
98109 reads the