annotate GEMBASSY-1.0.3/include/ghttp.c @ 0:8300eb051bea draft

Initial upload
author ktnyt
date Fri, 26 Jun 2015 05:19:29 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
1 /******************************************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
2 ** @source GEMBASSY http routines
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
3 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
4 ** @version 1.0
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
5 ** @modified December 27 2012 Hidetoshi Itaya Created this file
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
6 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
7 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
8 ** This library is free software; you can redistribute it and/or
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
9 ** modify it under the terms of the GNU Library General Public
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
10 ** License as published by the Free Software Foundation; either
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
11 ** version 2 of the License, or (at your option) any later version.
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
12 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
13 ** This library is distributed in the hope that it will be useful,
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
16 ** Library General Public License for more details.
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
17 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
18 ** You should have received a copy of the GNU Library General Public
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
19 ** License along with this library; if not, write to the
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
20 ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
21 ** Boston, MA 02111-1307, USA.
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
22 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
23
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
24 #include "ghttp.h"
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
25
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
26
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
27
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
28
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
29 /* @func gHttpConvertS ********************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
30 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
31 ** Converts result image to specified format
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
32 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
33 ** @param [r] url [AjPStr] URL to lookup
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
34 ** @param [r] outf [AjPFile*] File to write to
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
35 ** @return [AjBool]
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
36 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
37 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
38
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
39 AjBool gHttpConvertS(AjPStr url, AjPFile* outf, AjPStr informat, AjPStr outformat)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
40 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
41 AjPRegexp regexp = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
42 AjPStr jobid = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
43 AjPStr convert = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
44
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
45 regexp = ajRegCompC("^.+jobid=");
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
46
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
47 if(!ajRegExec(regexp, url))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
48 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
49 return ajFalse;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
50 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
51
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
52 if(!ajRegPost(regexp, &jobid))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
53 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
54 return ajFalse;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
55 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
56
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
57 convert = ajFmtStr("http://soap.g-language.org/WS/convert.cgi?"
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
58 "jobid=%S&informat=%S&outformat=%S",
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
59 jobid, informat, outformat);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
60
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
61 if(!gHttpGetBinS(convert, outf)) {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
62 return ajFalse;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
63 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
64
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
65 return ajTrue;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
66 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
67
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
68
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
69
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
70
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
71 /* @func gHttpConvertC ********************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
72 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
73 ** Converts result image to specified format
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
74 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
75 ** @param [r] url [AjPStr] URL to lookup
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
76 ** @param [r] outf [AjPFile*] File to write to
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
77 ** @return [AjBool]
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
78 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
79 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
80
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
81 AjBool gHttpConvertC(char* url, AjPFile* outf, AjPStr informat, AjPStr outformat)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
82 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
83 return gHttpConvertS(ajStrNewC(url), outf, informat, outformat);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
84 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
85
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
86
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
87
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
88
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
89 /* @func gHttpGetBinS *********************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
90 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
91 ** Writes out remote binary file to AjPFile
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
92 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
93 ** @param [r] url [AjPStr] URL to lookup
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
94 ** @param [r] outf [AjPFile*] File to write to
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
95 ** @return [AjBool]
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
96 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
97 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
98
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
99 AjBool gHttpGetBinS(AjPStr url, AjPFile* outf)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
100 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
101 AjPFile file = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
102 AjPStr line = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
103 AjPStr host = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
104 AjPStr path = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
105 AjPStr get = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
106 ajint port = 80;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
107 ajuint http = 0;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
108 FILE *fp;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
109
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
110 AjPRegexp crlf = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
111
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
112 char buf[8];
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
113
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
114 AjOSysSocket sock;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
115
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
116 get = ajStrNew();
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
117
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
118 ajHttpUrlDeconstruct(url, &port, &host, &path);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
119
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
120 while(file==NULL || gHttpRedirect(file, &host, &port, &path))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
121 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
122 if(ajStrGetCharFirst(path) != '/')
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
123 ajStrInsertK(&path, 0, '/');
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
124
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
125 ajFmtPrintS(&get, "GET http://%S:%d%S HTTP/1.1\r\n", host, port, path);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
126
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
127 fp = ajHttpOpen(NULL, host, port, get, &sock);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
128
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
129 file = ajFileNewFromCfile(fp);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
130
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
131 if(!file)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
132 return ajFalse;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
133 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
134
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
135 ajStrDel(&get);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
136
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
137 crlf = ajRegCompC("^\r?\n$");
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
138
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
139 while(ajReadline(file, &line))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
140 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
141 if(ajRegExec(crlf, line))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
142 break;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
143 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
144
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
145 while(ajReadbinBinary(file, 1, 1, buf))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
146 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
147 ajWritebinBinary(*outf, 1, 1, buf);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
148 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
149
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
150 ajFileClose(outf);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
151 ajFileClose(&file);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
152
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
153 return ajTrue;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
154 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
155
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
156
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
157
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
158
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
159 /* @func gHttpGetBinC *********************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
160 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
161 ** Retrives the C file pointer from a given URL
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
162 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
163 ** @param [r] url [char*] URL to lookup
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
164 ** @param [r] outf [AjPFile*] File to write to
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
165 ** @return [AjBool]
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
166 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
167 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
168
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
169 AjBool gHttpGetBinC(char* url, AjPFile* outf)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
170 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
171 if(!gHttpGetBinS(ajStrNewC(url), outf))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
172 return ajFalse;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
173
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
174 return ajTrue;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
175 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
176
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
177
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
178
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
179
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
180 /* @func gHttpPostSS **********************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
181 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
182 ** Post a file to url
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
183 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
184 ** @param [r] [AjPStr] URL to POST to
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
185 ** @param [r] [AjPStr] content to send
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
186 ** @return [AjPFilebuff]
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
187 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
188 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
189
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
190 AjPFilebuff gHttpPostFileSS(AjPStr url, AjPStr filename)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
191 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
192 AjPFilebuff buff = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
193 AjPFile file = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
194 AjPStr line = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
195 AjPStr cont = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
196 AjPStr host = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
197 AjPStr path = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
198 AjPStr post = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
199 AjPStr body = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
200 ajint port = 80;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
201 ajuint http = 0;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
202 FILE *fp;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
203
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
204 char crlf[] = "\015\021";
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
205
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
206 AjOSysSocket sock;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
207 AjOSysTimeout timo;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
208
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
209 post = ajStrNew();
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
210 body = ajStrNew();
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
211 cont = ajStrNew();
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
212
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
213 file = ajFileNewInNameS(filename);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
214
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
215 while(ajReadline(file, &line))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
216 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
217 ajStrAppendS(&cont, line);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
218 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
219
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
220 ajHttpUrlDeconstruct(url, &port, &host, &path);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
221
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
222 while(buff==NULL || ajHttpRedirect(buff, &host, &port, &path, &http))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
223 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
224 if(ajStrGetCharFirst(path) != '/')
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
225 ajStrInsertK(&path, 0, '/');
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
226
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
227 ajFmtPrintS(
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
228 &body,
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
229 "--xYzZY\015\012"
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
230 "Content-Disposition: form-data; name=\"file\";"
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
231 " filename=\"%S\"\015\012"
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
232 "Content-Type: text/plain\015\012"
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
233 "%S\015\012"
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
234 "\015\012--xYzZY--\015\012",
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
235 filename, cont
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
236 );
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
237
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
238 ajFmtPrintS(
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
239 &post,
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
240 "POST http://%S%S\n"
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
241 "Content-Length: %d\n"
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
242 "Content-Type: multipart/form-data; boundary=xYzZY\n\n"
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
243 "%S",
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
244 host, path,
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
245 ajStrGetLen(body), body
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
246 );
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
247
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
248 ajFmtPrint("%S", post);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
249
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
250 fp = ajHttpOpen(NULL, host, port, post, &sock);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
251
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
252 buff = ajFilebuffNewFromCfile(fp);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
253
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
254 if(!buff)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
255 return NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
256 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
257
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
258 ajStrDel(&post);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
259
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
260 timo.seconds = 180;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
261 ajSysTimeoutSet(&timo);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
262 ajFilebuffLoadAll(buff);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
263 ajSysTimeoutUnset(&timo);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
264
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
265 return buff;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
266 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
267
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
268
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
269
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
270
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
271 /* @func gHttpPostCS **********************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
272 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
273 ** Retrives the C file pointer from a given URL
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
274 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
275 ** @param [r] [char*] URL to lookup
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
276 ** @return [FILE*]
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
277 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
278 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
279
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
280 AjPFilebuff gHttpPostFileCS(char* url, AjPStr filename)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
281 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
282 AjPFilebuff buff = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
283
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
284 buff = gHttpPostFileSS(ajStrNewC(url), filename);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
285
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
286 if(!buff)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
287 return NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
288
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
289 return buff;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
290 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
291
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
292
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
293
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
294
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
295 /* @func gHttpRedirect ********************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
296 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
297 ** Reads the header of http response in given buffer buff,
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
298 ** if it includes a redirection response updates the host, port and get
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
299 ** parameters using the 'Location' header
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
300 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
301 ** @param [u] buff [FILE*] file pointer
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
302 ** @param [w] host [AjPStr*] Host name
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
303 ** @param [w] port [ajint*] Port
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
304 ** @param [w] path [AjPStr*] part of URL after port number
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
305 ** @return [AjBool] returns true if the header includes a redirection response
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
306 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
307 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
308
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
309 AjBool gHttpRedirect(AjPFile file, AjPStr* host, ajint* port, AjPStr* path)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
310 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
311 AjPFilebuff buff = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
312
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
313 AjPRegexp httpexp = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
314 AjPRegexp nullexp = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
315 AjPRegexp redirexp = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
316
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
317 AjPStr codestr = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
318 AjPStr newurl = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
319 AjPStr newhost = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
320 AjPStr currline = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
321
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
322 ajuint httpcode = 0;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
323
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
324 AjBool isheader = ajFalse;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
325 AjBool ret = ajFalse;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
326
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
327 httpexp = ajRegCompC("^HTTP/\\S+\\s+(\\d+)");
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
328
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
329 ajReadline(file, &currline);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
330
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
331 ajDebug("gHttpRedirect: First line: '%S'\n", currline);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
332
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
333 if(ajRegExec(httpexp, currline))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
334 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
335 isheader = ajTrue;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
336 ajRegSubI(httpexp, 1, &codestr);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
337 ajStrToUint(codestr, &httpcode);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
338 ajDebug("Header: codestr '%S' code '%u'\n", codestr, httpcode);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
339 ajStrDel(&codestr);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
340 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
341
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
342 if(isheader)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
343 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
344 if(httpcode == 301 || httpcode == 302 || httpcode==307)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
345 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
346 redirexp = ajRegCompC("^Location: (\\S+)");
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
347 nullexp = ajRegCompC("^\r?\n?$");
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
348
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
349 while( ajReadline(file, &currline) &&
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
350 !ajRegExec(nullexp, currline))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
351 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
352 ajDebug("gHttpRedirect: header line: '%S'\n", currline);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
353
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
354 if(ajRegExec(redirexp, currline))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
355 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
356 ajRegSubI(redirexp, 1, &newurl);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
357 ajHttpUrlDeconstruct(newurl, port, &newhost, path);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
358
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
359 if(ajStrGetLen(newhost))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
360 ajStrAssignS(host, newhost);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
361
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
362 ajStrDel(&newurl);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
363 ajStrDel(&newhost);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
364 ret = ajTrue;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
365 break;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
366 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
367 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
368
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
369 ajRegFree(&redirexp);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
370 ajRegFree(&nullexp);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
371 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
372 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
373
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
374 ajRegFree(&httpexp);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
375 ajStrDel(&currline);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
376
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
377 return ret;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
378 }