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