BrowserWindow.bjs
[app.webkitpdf] / Request.vala
1
2 /**
3
4 idea is a async http request.
5
6 -- question is are we creating a generic request.. or just doing a silly wrapper of Soup?
7
8
9 x=  new Request(url)
10 x.url = 
11 x.connect.complete((headers, body, body_len) => {
12   .. what to do next
13 });
14
15 x.run(); // returns instantly...
16
17
18 Usage:
19 a - json fetches from main server.. (async not needed)
20 b - head request on page to see if it's changed -- async usefull - we might want to be doing a few at the same time..
21 c - body requests on a page... async usefull - we might want to be doing a few at the same time..
22
23
24
25 valac  --thread  -g  Request.vala --pkg glib-2.0 --pkg gee-1.0 --pkg libsoup-2.4 --pkg gio-2.0 -o /tmp/req --target-glib=2.32  -X -lm -X -pg
26
27
28 */
29
30 void main () {
31         var loop = new MainLoop();
32         var x = new Request("HEAD", "http://jobsonboats.com/");
33         x.complete.connect((uri, headers, body, body_len) => {
34                 print("ct:  %s", headers.get("Content-type"));
35                 print("got body %d\n", (int)body_len);
36                 loop.quit();
37         });
38         x.send();
39         loop.run();
40 }
41
42 public class Request : Object
43 {
44
45         public string url;
46         public string method = "GET";
47         public signal void complete( Soup.URI uri, Gee.HashMap<string,string> head, uint8[]? body, int64 length);
48
49         public Request(string method, string url)
50         {
51                 this.method = method;
52                 this.url = url;
53         }
54  
55
56         public void send()
57         {
58
59                 Soup.Session session = new Soup.Session ();
60                 
61                 session.use_thread_context = true;
62                 
63                 var msg = new Soup.Message(this.method, this.url);
64                 
65                 var headers= new Gee.HashMap<string,string>();
66                 
67                 session.queue_message (msg, (obj, mess) => {
68                         print ("Status Code: %u\n", mess.status_code);
69                         print ("Final URL: %s\n", mess.uri.to_string (false));
70                 
71                          mess.response_headers.foreach ((name, val) => {
72                                 print("HEADER %s: %s\n", name,val);
73                                 headers.set(name, val);
74                         });
75                         if (this.method == "HEAD") {
76                                 this.complete(mess.uri, headers, null, 0); 
77                         }
78                         this.complete(mess.uri, headers, mess.response_body.data, mess.response_body.length);
79                                  
80                 });
81                 
82         }
83  
84
85 }