annotate GEMBASSY-1.0.3/include/gpost.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 The last resort for POSTing
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 "gpost.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 gFilePostCC **********************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
30 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
31 ** Posts the file to given url
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
32 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
33 ** @param [r] [char*] URL to lookup
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
34 ** @return [AjPFile]
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
35 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
36 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
37
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
38 AjBool gFilePostCC(char* url, char* filename, AjPStr* string)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
39 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
40 CURL *curl;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
41 CURLcode res;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
42
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
43 struct curl_httppost *post = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
44 struct curl_httppost *last = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
45
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
46 Memory *mem = malloc(sizeof(Memory*));
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
47
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
48 mem->size = 0;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
49 mem->memory = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
50
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
51 curl = curl_easy_init();
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
52
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
53 if(curl)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
54 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
55 curl_formadd(&post, &last,
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
56 CURLFORM_COPYNAME, "file",
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
57 CURLFORM_FILE, filename,
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
58 CURLFORM_END);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
59
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
60 curl_easy_setopt(curl, CURLOPT_URL, url);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
61 curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
62 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
63 curl_easy_setopt(curl, CURLOPT_WRITEDATA, mem);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
64
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
65 res = curl_easy_perform(curl);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
66
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
67 if(res)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
68 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
69 return 0;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
70 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
71
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
72 curl_formfree(post);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
73 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
74 else
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
75 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
76 return 0;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
77 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
78
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
79 curl_easy_cleanup(curl);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
80
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
81 ajStrAssignC(string, mem->memory);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
82
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
83 return 1;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
84 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
85
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
86 size_t curl_write(void* ptr, size_t size, size_t nmemb, void* data)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
87 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
88 if(size * nmemb == 0)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
89 return 0;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
90
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
91 size_t realsize = size * nmemb;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
92 Memory* mem = (Memory*)data;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
93 mem->memory = (char*)realloc(mem->memory,mem->size + realsize + 1);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
94 if(mem->memory){
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
95 memcpy(&(mem->memory[mem->size]),ptr,realsize);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
96 mem->size += realsize;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
97 mem->memory[mem->size] = 0;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
98 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
99
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
100 return realsize;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
101 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
102
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
103
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
104
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
105 /* @func gFilePostCS **********************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
106 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
107 ** Posts the file to given url
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
108 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
109 ** @param [r] [char*] URL to lookup
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
110 ** @return [AjPFile]
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
111 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
112 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
113
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
114 AjBool gFilePostCS(char* url, AjPStr filename, AjPStr* string)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
115 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
116 if(!gFilePostCC(url, ajCharNewS(filename), string))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
117 return ajFalse;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
118
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
119 return ajTrue;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
120 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
121
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
122
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
123
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
124 /* @func gFilePostSS **********************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
125 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
126 ** Posts the file to given url
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
127 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
128 ** @param [r] [char*] URL to lookup
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
129 ** @return [AjPFile]
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
130 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
131 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
132
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
133 AjBool gFilePostSS(AjPStr url, AjPStr filename, AjPStr* string)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
134 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
135 if(!gFilePostCC(ajCharNewS(url), ajCharNewS(filename), string))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
136 return ajFalse;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
137
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
138 return ajTrue;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
139 }