Allow using custom method names in wxHTTP.
[wxWidgets.git] / interface / wx / protocol / http.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: protocol/http.h
3 // Purpose: interface of wxHTTP
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 /**
10 @class wxHTTP
11
12 wxHTTP can be used to establish a connection to an HTTP server.
13
14 wxHTTP can thus be used to create a (basic) HTTP @b client.
15
16 @library{wxnet}
17 @category{net}
18
19 @see wxSocketBase, wxURL
20 */
21 class wxHTTP : public wxProtocol
22 {
23 public:
24 /**
25 Default constructor.
26 */
27 wxHTTP();
28
29 /**
30 Destructor will close the connection if connected.
31 */
32 virtual ~wxHTTP();
33
34 //@{
35 /**
36 Connect to the HTTP server.
37
38 By default, connection is made to the port 80 of the specified @a host.
39 You may connect to a non-default port by specifying it explicitly using
40 the second overload.
41
42 Currently wxHTTP only supports IPv4.
43
44 For the overload taking wxSockAddress, the @a wait argument is ignored.
45 */
46 virtual bool Connect(const wxString& host);
47 virtual bool Connect(const wxString& host, unsigned short port);
48 virtual bool Connect(const wxSockAddress& addr, bool wait);
49 //@}
50
51 /**
52 Returns the data attached with a field whose name is specified by @a header.
53 If the field doesn't exist, it will return an empty string and not a @NULL string.
54
55 @note
56 The header is not case-sensitive, i.e. "CONTENT-TYPE" and "content-type"
57 represent the same header.
58 */
59 wxString GetHeader(const wxString& header) const;
60
61 /**
62 Creates a new input stream on the specified path.
63
64 Notice that this stream is unseekable, i.e. SeekI() and TellI() methods
65 shouldn't be used.
66
67 Note that you can still know the size of the file you are getting using
68 wxStreamBase::GetSize(). However there is a limitation: in HTTP protocol,
69 the size is not always specified so sometimes @c (size_t)-1 can returned to
70 indicate that the size is unknown.
71 In such case, you may want to use wxInputStream::LastRead() method in a loop
72 to get the total size.
73
74 @return Returns the initialized stream. You must delete it yourself
75 once you don't use it anymore and this must be done before
76 the wxHTTP object itself is destroyed. The destructor
77 closes the network connection. The next time you will
78 try to get a file the network connection will have to
79 be reestablished, but you don't have to take care of
80 this since wxHTTP reestablishes it automatically.
81
82 @see wxInputStream
83 */
84 virtual wxInputStream* GetInputStream(const wxString& path);
85
86 /**
87 Returns the HTTP response code returned by the server.
88
89 Please refer to RFC 2616 for the list of responses.
90 */
91 int GetResponse() const;
92
93 /**
94 Set HTTP method.
95
96 Set <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html">common</a>
97 or expanded HTTP method.
98
99 Overrides GET or POST methods that is used by default.
100
101 @param method
102 HTTP method name, e.g. "GET".
103
104 @since 3.0.0
105
106 @see SetPostBuffer(), SetPostText()
107 */
108 void SetMethod(const wxString& method);
109
110 /**
111 It sets data of a field to be sent during the next request to the HTTP server.
112
113 The field name is specified by @a header and the content by @a h_data.
114 This is a low level function and it assumes that you know what you are doing.
115 */
116 void SetHeader(const wxString& header, const wxString& h_data);
117
118 /**
119 Returns the value of a cookie.
120 */
121
122 wxString GetCookie(const wxString& cookie) const;
123
124 /**
125 Returns @true if there were cookies.
126 */
127 bool HasCookies() const;
128
129 /**
130 Set the binary data to be posted to the server.
131
132 If a non-empty buffer is passed to this method, the next request will
133 be an HTTP @c POST instead of the default HTTP @c GET and the given @a
134 data will be posted as the body of this request.
135
136 For textual data a more convenient SetPostText() can be used instead.
137
138 @param contentType
139 The value of HTTP "Content-Type" header, e.g. "image/png".
140 @param data
141 The data to post.
142 @return
143 @true if any data was passed in or @false if the buffer was empty.
144
145 @since 2.9.4
146 */
147 bool SetPostBuffer(const wxString& contentType, const wxMemoryBuffer& data);
148
149 /**
150 Set the text to be posted to the server.
151
152 After a successful call to this method, the request will use HTTP @c
153 POST instead of the default @c GET when it's executed.
154
155 Use SetPostBuffer() if you need to post non-textual data.
156
157 @param contentType
158 The value of HTTP "Content-Type" header, e.g. "text/html;
159 charset=UTF-8".
160 @param data
161 The data to post.
162 @param conv
163 The conversion to use to convert @a data contents to a byte stream.
164 Its value should be consistent with the charset parameter specified
165 in @a contentType.
166 @return
167 @true if string was non-empty and was successfully converted using
168 the given @a conv or @false otherwise (in this case this request
169 won't be @c POST'ed correctly).
170
171 @since 2.9.4
172 */
173 bool SetPostText(const wxString& contentType,
174 const wxString& data,
175 const wxMBConv& conv = wxConvUTF8);
176 };
177