132
|
1 /*******************************************************************************
|
|
2
|
|
3 copyright: Copyright (c) 2004 Kris Bell. All rights reserved
|
|
4
|
|
5 license: BSD style: $(LICENSE)
|
|
6
|
|
7 version: Initial release: January 2006
|
|
8
|
|
9 author: Kris
|
|
10
|
|
11 *******************************************************************************/
|
|
12
|
|
13 module tango.net.http.HttpGet;
|
|
14
|
|
15 public import tango.net.Uri;
|
|
16
|
|
17 private import tango.io.GrowBuffer;
|
|
18
|
|
19 private import tango.net.http.HttpClient,
|
|
20 tango.net.http.HttpHeaders;
|
|
21
|
|
22 /*******************************************************************************
|
|
23
|
|
24 Supports the basic needs of a client making requests of an HTTP
|
|
25 server. The following is a usage example:
|
|
26 ---
|
|
27 // open a web-page for reading (see HttpPost for writing)
|
|
28 auto page = new HttpGet ("http://www.digitalmars.com/d/intro.html");
|
|
29
|
|
30 // retrieve and flush display content
|
|
31 Cout (cast(char[]) page.read) ();
|
|
32 ---
|
|
33
|
|
34 *******************************************************************************/
|
|
35
|
|
36 class HttpGet : HttpClient
|
|
37 {
|
|
38 private GrowBuffer buffer;
|
|
39
|
|
40 /***********************************************************************
|
|
41
|
|
42 Create a client for the given URL. The argument should be
|
|
43 fully qualified with an "http:" or "https:" scheme, or an
|
|
44 explicit port should be provided.
|
|
45
|
|
46 ***********************************************************************/
|
|
47
|
|
48 this (char[] url, uint pageChunk = 16 * 1024)
|
|
49 {
|
|
50 this (new Uri(url), pageChunk);
|
|
51 }
|
|
52
|
|
53 /***********************************************************************
|
|
54
|
|
55 Create a client with the provided Uri instance. The Uri should
|
|
56 be fully qualified with an "http:" or "https:" scheme, or an
|
|
57 explicit port should be provided.
|
|
58
|
|
59 ***********************************************************************/
|
|
60
|
|
61 this (Uri uri, uint pageChunk = 16 * 1024)
|
|
62 {
|
|
63 super (HttpClient.Get, uri);
|
|
64 buffer = new GrowBuffer (pageChunk, pageChunk);
|
|
65 }
|
|
66
|
|
67 /***********************************************************************
|
|
68
|
|
69 ***********************************************************************/
|
|
70
|
|
71 void[] read ()
|
|
72 {
|
|
73 try {
|
|
74 buffer.clear;
|
|
75 open (buffer);
|
|
76 if (isResponseOK)
|
|
77 buffer.fill (getResponseHeaders.getInt(HttpHeader.ContentLength, uint.max));
|
|
78 } finally {close;}
|
|
79 return buffer.slice;
|
|
80 }
|
|
81 }
|
|
82
|