Parent Directory | Revision Log
Gcvote xml parsing started
1 | harbaum | 157 | /* |
2 | * This file is part of GPXView. | ||
3 | * | ||
4 | * GPXView is free software: you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation, either version 3 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * GPXView is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with GPXView. If not, see <http://www.gnu.org/licenses/>. | ||
16 | */ | ||
17 | harbaum | 152 | |
18 | harbaum | 157 | |
19 | /* curl command line example: | ||
20 | curl -d version=2.0b -duserName=uglyDUMMYusernamesolution -d cacheIds=4e68a04e-161c-4351-9813-bec6f6a4ca8a -d password= http://dosensuche.de/GCVote/getVotes.php | ||
21 | */ | ||
22 | |||
23 | #include <curl/curl.h> | ||
24 | #include <curl/types.h> | ||
25 | #include <curl/easy.h> | ||
26 | |||
27 | #include <libxml/parser.h> | ||
28 | #include <libxml/tree.h> | ||
29 | |||
30 | #ifndef LIBXML_TREE_ENABLED | ||
31 | #error "Tree not enabled in libxml" | ||
32 | #endif | ||
33 | |||
34 | #include "gpxview.h" | ||
35 | |||
36 | #define ID_PATTERN "guid=" | ||
37 | |||
38 | typedef struct { | ||
39 | char *ptr; | ||
40 | int len; | ||
41 | } curl_mem_t; | ||
42 | |||
43 | static size_t mem_write(void *ptr, size_t size, size_t nmemb, | ||
44 | void *stream) { | ||
45 | curl_mem_t *p = (curl_mem_t*)stream; | ||
46 | |||
47 | p->ptr = g_realloc(p->ptr, p->len + size*nmemb + 1); | ||
48 | if(p->ptr) { | ||
49 | memcpy(p->ptr+p->len, ptr, size*nmemb); | ||
50 | p->len += size*nmemb; | ||
51 | p->ptr[p->len] = 0; | ||
52 | } | ||
53 | return nmemb; | ||
54 | } | ||
55 | |||
56 | #if 0 | ||
57 | #define PROXY_KEY "/system/http_proxy/" | ||
58 | |||
59 | typedef struct { | ||
60 | char *authentication_password, *authentication_user; | ||
61 | char *host; | ||
62 | char *ignore_hosts; | ||
63 | gint port; | ||
64 | gboolean use_authentication; | ||
65 | } proxy_t; | ||
66 | |||
67 | void net_io_set_proxy(CURL *curl, proxy_t *proxy) { | ||
68 | /* ------ overwrite with settings from gconf if present ------- */ | ||
69 | GConfClient *client = gconf_client_get_default(); | ||
70 | |||
71 | /* ------------- get proxy settings -------------------- */ | ||
72 | if(gconf_client_get_bool(client, PROXY_KEY "use_http_proxy", NULL)) { | ||
73 | proxy_t *proxy = settings->proxy = g_new0(proxy_t, 1); | ||
74 | |||
75 | /* get basic settings */ | ||
76 | proxy->host = gconf_client_get_string(client, PROXY_KEY "host", NULL); | ||
77 | proxy->port = gconf_client_get_int(client, PROXY_KEY "port", NULL); | ||
78 | proxy->ignore_hosts = | ||
79 | gconf_client_get_string(client, PROXY_KEY "ignore_hosts", NULL); | ||
80 | |||
81 | /* check for authentication */ | ||
82 | proxy->use_authentication = | ||
83 | gconf_client_get_bool(client, PROXY_KEY "use_authentication", NULL); | ||
84 | |||
85 | if(proxy->use_authentication) { | ||
86 | proxy->authentication_user = | ||
87 | gconf_client_get_string(client, PROXY_KEY "authentication_user", NULL); | ||
88 | proxy->authentication_password = | ||
89 | gconf_client_get_string(client, PROXY_KEY "authentication_password", | ||
90 | NULL); | ||
91 | } | ||
92 | } | ||
93 | |||
94 | if(proxy) { | ||
95 | if(proxy->ignore_hosts) | ||
96 | printf("WARNING: Pproxy \"ignore_hosts\" unsupported!\n"); | ||
97 | |||
98 | printf("net_io: using proxy %s:%d\n", proxy->host, proxy->port); | ||
99 | |||
100 | curl_easy_setopt(curl, CURLOPT_PROXY, proxy->host); | ||
101 | curl_easy_setopt(curl, CURLOPT_PROXYPORT, proxy->port); | ||
102 | |||
103 | if(proxy->use_authentication) { | ||
104 | printf("net_io: use auth for user %s\n", proxy->authentication_user); | ||
105 | |||
106 | char *cred = g_strdup_printf("%s:%s", | ||
107 | proxy->authentication_user, | ||
108 | proxy->authentication_password); | ||
109 | |||
110 | curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, cred); | ||
111 | g_free(cred); | ||
112 | } | ||
113 | } | ||
114 | } | ||
115 | #endif | ||
116 | |||
117 | /* parse root element */ | ||
118 | static void parse_root(xmlDocPtr doc, xmlNode *a_node) { | ||
119 | xmlNode *cur_node = NULL; | ||
120 | |||
121 | for (cur_node = a_node; cur_node; cur_node = cur_node->next) { | ||
122 | if (cur_node->type == XML_ELEMENT_NODE) { | ||
123 | |||
124 | if(strcasecmp((char*)cur_node->name, "WMT_MS_Capabilities") == 0) { | ||
125 | printf("xyz\n"); | ||
126 | |||
127 | // wms_cap_parse(wms, doc, cur_node); | ||
128 | } else | ||
129 | printf("found unhandled %s\n", cur_node->name); | ||
130 | } | ||
131 | } | ||
132 | } | ||
133 | |||
134 | static void parse_doc(xmlDocPtr doc) { | ||
135 | /* Get the root element node */ | ||
136 | xmlNode *root_element = xmlDocGetRootElement(doc); | ||
137 | |||
138 | parse_root(doc, root_element); | ||
139 | |||
140 | /*free the document */ | ||
141 | xmlFreeDoc(doc); | ||
142 | |||
143 | /* | ||
144 | * Free the global variables that may | ||
145 | * have been allocated by the parser. | ||
146 | */ | ||
147 | xmlCleanupParser(); | ||
148 | } | ||
149 | |||
150 | int gcvote_get(appdata_t *appdata, char *url) { | ||
151 | CURL *curl; | ||
152 | struct curl_httppost *formpost=NULL; | ||
153 | struct curl_httppost *lastptr=NULL; | ||
154 | CURLcode res; | ||
155 | curl_mem_t mem = { NULL, 0 }; | ||
156 | |||
157 | char *id = NULL; | ||
158 | |||
159 | if(!url) return GCVOTE_NONE; | ||
160 | |||
161 | id = strstr(url, ID_PATTERN); | ||
162 | if(!id) return GCVOTE_NONE; | ||
163 | |||
164 | id += strlen(ID_PATTERN); | ||
165 | |||
166 | printf("request to get votes for id %s\n", id); | ||
167 | |||
168 | curl_formadd(&formpost, &lastptr, | ||
169 | CURLFORM_COPYNAME, "version", | ||
170 | CURLFORM_COPYCONTENTS, APP VERSION, | ||
171 | CURLFORM_END); | ||
172 | |||
173 | curl_formadd(&formpost, &lastptr, | ||
174 | CURLFORM_COPYNAME, "cacheIds", | ||
175 | CURLFORM_COPYCONTENTS, id, | ||
176 | CURLFORM_END); | ||
177 | |||
178 | curl = curl_easy_init(); | ||
179 | if(!curl) return GCVOTE_NONE; | ||
180 | |||
181 | /* play nice and report some user agent */ | ||
182 | curl_easy_setopt(curl, CURLOPT_USERAGENT, APP " " VERSION); | ||
183 | |||
184 | /* download to memory */ | ||
185 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, &mem); | ||
186 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, mem_write); | ||
187 | |||
188 | /* what URL that receives this POST */ | ||
189 | curl_easy_setopt(curl, CURLOPT_URL, "http://dosensuche.de/GCVote/getVotes.php"); | ||
190 | curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); | ||
191 | res = curl_easy_perform(curl); | ||
192 | |||
193 | /* always cleanup */ | ||
194 | curl_easy_cleanup(curl); | ||
195 | |||
196 | /* then cleanup the formpost chain */ | ||
197 | curl_formfree(formpost); | ||
198 | |||
199 | printf("received %d bytes\n", mem.len); | ||
200 | printf("data: %s\n", mem.ptr); | ||
201 | |||
202 | /* start XML parser */ | ||
203 | LIBXML_TEST_VERSION; | ||
204 | |||
205 | /* parse the file and get the DOM */ | ||
206 | xmlDoc *doc = xmlReadMemory(mem.ptr, mem.len, NULL, NULL, 0); | ||
207 | if(!doc) { | ||
208 | g_free(mem.ptr); | ||
209 | return GCVOTE_NONE; | ||
210 | } | ||
211 | |||
212 | printf("ok, parse doc tree\n"); | ||
213 | parse_doc(doc); | ||
214 | |||
215 | |||
216 | return 0; | ||
217 | } |