comparison tango/tango/net/http/HttpPost.d @ 132:1700239cab2e trunk

[svn r136] MAJOR UNSTABLE UPDATE!!! Initial commit after moving to Tango instead of Phobos. Lots of bugfixes... This build is not suitable for most things.
author lindquist
date Fri, 11 Jan 2008 17:57:40 +0100
parents
children
comparison
equal deleted inserted replaced
131:5825d48b27d1 132:1700239cab2e
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.HttpPost;
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 sending POST requests to a
25 HTTP server. The following is a usage example:
26
27 ---
28 // open a web-page for posting (see HttpGet for simple reading)
29 auto post = new HttpPost ("http://yourhost/yourpath");
30
31 // send, retrieve and display response
32 Cout (cast(char[]) post.write("posted data", "text/plain"));
33 ---
34
35 *******************************************************************************/
36
37 class HttpPost : HttpClient
38 {
39 private GrowBuffer buffer;
40
41 /***********************************************************************
42
43 Create a client for the given URL. The argument should be
44 fully qualified with an "http:" or "https:" scheme, or an
45 explicit port should be provided.
46
47 ***********************************************************************/
48
49 this (char[] url, uint pageChunk = 16 * 1024)
50 {
51 this (new Uri(url), pageChunk);
52 }
53
54 /***********************************************************************
55
56 Create a client with the provided Uri instance. The Uri should
57 be fully qualified with an "http:" or "https:" scheme, or an
58 explicit port should be provided.
59
60 ***********************************************************************/
61
62 this (Uri uri, uint pageChunk = 16 * 1024)
63 {
64 super (HttpClient.Post, uri);
65 buffer = new GrowBuffer (pageChunk, pageChunk);
66 }
67
68 /***********************************************************************
69
70 Send query params only
71
72 ***********************************************************************/
73
74 void[] write ()
75 {
76 return write (null);
77 }
78
79 /***********************************************************************
80
81 Send content and no query params. The contentLength header
82 will be set to match the provided content, and contentType
83 set to the given type.
84
85 ***********************************************************************/
86
87 void[] write (void[] content, char[] type)
88 {
89 auto headers = getRequestHeaders();
90
91 headers.add (HttpHeader.ContentType, type);
92 headers.addInt (HttpHeader.ContentLength, content.length);
93
94 return write (delegate void (IBuffer b){b.append(content);});
95 }
96
97 /***********************************************************************
98
99 Send raw data via the provided pump, and no query
100 params. You have full control over headers and so
101 on via this method.
102
103 ***********************************************************************/
104
105 void[] write (Pump pump)
106 {
107 try {
108 buffer.clear;
109 open (pump, buffer);
110
111 // check return status for validity
112 auto status = getStatus();
113 if (status is HttpResponseCode.OK ||
114 status is HttpResponseCode.Created ||
115 status is HttpResponseCode.Accepted)
116 buffer.fill (getResponseHeaders.getInt (HttpHeader.ContentLength, uint.max));
117 } finally {close;}
118
119 return buffer.slice;
120 }
121 }
122