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