source: subversion/applications/lib/libosm/Client.cpp @ 11743

Last change on this file since 11743 was 11681, checked in by nienhueser, 11 years ago

Have curl follow redirects, needed for the informationhighway server and others that redirect to suitable servers

File size: 4.4 KB
Line 
1#include "Client.h"
2
3#include <cstdio>
4#include <cstdlib>
5#include <cstring>
6
7
8/*
9Copyright (C) 2005 Nick Whitelegg, Hogweed Software, nick@hogweed.org
10
11This program is free software; you can redistribute it and/or modify
12it under the terms of the GNU Lesser General Public License as published by
13the Free Software Foundation; either version 2 of the License, or
14(at your option) any later version.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19GNU Lesser General Public License for more details.
20
21You should have received a copy of the GNU Lesser General Public License
22along with this program; if not, write to the Free Software
23Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
24
25*/
26
27
28/* CREDIT: The approach taken (of a struct storing the read-in-data and its
29* size) is inspired by the callback example on the cURL web site. */
30
31namespace OSM
32{
33
34Client::Client(const std::string& urlbase)
35{
36        this->urlbase=urlbase;
37        username="";
38        password="";
39}
40
41void Client::setLoginDetails(const std::string& u, const std::string& p)
42{
43        username=u;
44        password=p;
45}
46
47std::string  Client::grabOSM(const char *apicall,
48                                        double west,double south,double east,double north)
49{
50        char url[1024];
51        sprintf(url,"%s/%s?bbox=%lf,%lf,%lf,%lf",
52                                        urlbase.c_str(),apicall,west,south,east,north);
53        return grab(url);
54}
55
56std::string  Client::grabOSM(const char *apicall)
57{
58        char url[1024];
59        sprintf(url,"%s/%s", urlbase.c_str(),apicall);
60        return grab(url);
61}
62
63std::string  Client::grab(const char *url)
64{
65        std::string data;
66
67        fprintf(stderr, "grab(): URL=%s\n",url);
68        CURL *curl =  curl_easy_init();
69
70
71        if(curl)
72        {
73                data = doGrab(curl,url);
74                curl_easy_cleanup(curl);
75                return data;
76        }
77        return "";
78}
79
80std::string  Client::doGrab(CURL *curl,const char *url)
81{
82        char uname_pwd[1024];
83        CURLcode res;
84        std::string returned="";
85
86        if(username!="" && password!="")
87        {
88                Data *data = (Data *)malloc(sizeof(Data));
89                data->data = NULL;
90                data->nbytes = 0;
91
92                sprintf(uname_pwd,"%s:%s",username.c_str(),password.c_str());
93                curl_easy_setopt(curl,CURLOPT_USERPWD,uname_pwd);
94
95                curl_easy_setopt(curl,CURLOPT_URL,url);
96                curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1);
97                curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,Client::responseCallback);
98                curl_easy_setopt(curl,CURLOPT_WRITEDATA,data);
99
100                res=curl_easy_perform(curl);
101
102                fprintf(stderr,"Got data.\n");
103
104                data->data=(char *)realloc(data->data,(data->nbytes+1)*sizeof(char));
105                data->data[data->nbytes] = '\0';
106                returned = data->data;
107                free(data->data);
108                free(data);
109        }
110
111        return returned;
112}
113
114size_t Client::responseCallback(void *ptr,size_t size,size_t nmemb,void *d)
115{
116        size_t rsize=size*nmemb;
117        Data *data=(Data *)d;
118        data->data=(char *)realloc(data->data,(data->nbytes+rsize)
119                                *sizeof(char));
120        memcpy(&(data->data[data->nbytes]),ptr,rsize);
121        data->nbytes += rsize;
122        //      fprintf(stderr,"data->nbytes is %d\n", data->nbytes);
123        return rsize;
124}
125
126size_t Client::putCallback
127                (void *bufptr,size_t size, size_t nitems, void *userp)
128{
129        char *p1= (char*)bufptr, *p2=(char*)userp;
130        //      fprintf(stderr,"callback: userp=%s\n",(char *)userp);
131        strcpy(p1,p2);
132        return strlen(p1)+1;
133}
134
135std::string Client::putToOSM(char* apicall, char* idata)
136{
137        CURL *curl;
138
139        char uname_pwd[1024];
140        char url[1024];
141        std::string returned="";
142        if(username!="" && password!="")
143        {
144                sprintf(uname_pwd,"%s:%s",username.c_str(),password.c_str());
145                sprintf(url,"%s/%s",urlbase.c_str(),apicall);
146
147                Data *odata = (Data *)malloc(sizeof(Data));
148                odata->data = NULL;
149                odata->nbytes = 0;
150
151                curl_global_init(CURL_GLOBAL_ALL);
152                curl = curl_easy_init();
153
154                if(curl)
155                {
156                        curl_easy_setopt(curl,CURLOPT_READFUNCTION,Client::putCallback);
157                        curl_easy_setopt(curl,CURLOPT_UPLOAD, true);
158                        curl_easy_setopt(curl,CURLOPT_PUT, true);
159                        curl_easy_setopt(curl,CURLOPT_URL, url);
160                        curl_easy_setopt(curl,CURLOPT_USERPWD,uname_pwd);
161                        curl_easy_setopt(curl,CURLOPT_READDATA,idata);
162                        curl_easy_setopt(curl,CURLOPT_INFILESIZE,(long)(strlen(idata)+1));
163                        curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,Client::putCallback);
164                        curl_easy_setopt(curl,CURLOPT_WRITEDATA,odata);
165                        curl_easy_perform(curl);
166                        curl_easy_cleanup(curl);
167                        odata->data=(char *)realloc
168                                (odata->data,(odata->nbytes+1)*sizeof(char));
169                        odata->data[odata->nbytes] = '\0';
170                        returned = odata->data;
171                        free(odata->data);
172                }
173
174                curl_global_cleanup();
175                free(odata);
176        }
177        return returned;
178}
179
180}
Note: See TracBrowser for help on using the repository browser.