retriever.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009, Willow Garage, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
29 
30 #include <string.h>
31 
32 #include <ros/package.h>
33 #include <ros/console.h>
34 
35 #include <curl/curl.h>
36 
37 namespace resource_retriever
38 {
39 
41 {
42 public:
44  : initialized_(false)
45  {
46  CURLcode ret = curl_global_init(CURL_GLOBAL_ALL);
47  if (ret != 0)
48  {
49  ROS_ERROR("Error initializing libcurl! retcode = %d", ret);
50  }
51  else
52  {
53  initialized_ = true;
54  }
55  }
56 
58  {
59  if (initialized_)
60  {
61  curl_global_cleanup();
62  }
63  }
64 
66 };
68 
70 {
71  curl_handle_ = curl_easy_init();
72 }
73 
75 {
76  if (curl_handle_)
77  {
78  curl_easy_cleanup(curl_handle_);
79  }
80 }
81 
83 {
84  std::vector<uint8_t> v;
85 };
86 
87 size_t curlWriteFunc(void* buffer, size_t size, size_t nmemb, void* userp)
88 {
89  MemoryBuffer* membuf = (MemoryBuffer*)userp;
90 
91  size_t prev_size = membuf->v.size();
92  membuf->v.resize(prev_size + size * nmemb);
93  memcpy(&membuf->v[prev_size], buffer, size * nmemb);
94 
95  return size * nmemb;
96 }
97 
98 MemoryResource Retriever::get(const std::string& url)
99 {
100  std::string mod_url = url;
101  if (url.find("package://") == 0)
102  {
103  mod_url.erase(0, strlen("package://"));
104  size_t pos = mod_url.find("/");
105  if (pos == std::string::npos)
106  {
107  throw Exception(url, "Could not parse package:// format into file:// format");
108  }
109 
110  std::string package = mod_url.substr(0, pos);
111  mod_url.erase(0, pos);
112  std::string package_path = ros::package::getPath(package);
113 
114  if (package_path.empty())
115  {
116  throw Exception(url, "Package [" + package + "] does not exist");
117  }
118 
119  mod_url = "file://" + package_path + mod_url;
120  }
121 
122  curl_easy_setopt(curl_handle_, CURLOPT_URL, mod_url.c_str());
123  curl_easy_setopt(curl_handle_, CURLOPT_WRITEFUNCTION, curlWriteFunc);
124 
125  char error_buffer[CURL_ERROR_SIZE];
126  curl_easy_setopt(curl_handle_, CURLOPT_ERRORBUFFER , error_buffer);
127 
128  MemoryResource res;
129  MemoryBuffer buf;
130  curl_easy_setopt(curl_handle_, CURLOPT_WRITEDATA, &buf);
131 
132  CURLcode ret = curl_easy_perform(curl_handle_);
133  if (ret != 0)
134  {
135  throw Exception(mod_url, error_buffer);
136  }
137  else if (!buf.v.empty())
138  {
139  res.size = buf.v.size();
140  res.data.reset(new uint8_t[res.size+1]);
141  memcpy(res.data.get(), &buf.v[0], res.size);
142  res.data[res.size] = '\0'; // write trailing zero for interpretation as char*
143  }
144 
145  return res;
146 }
147 
148 }
size_t curlWriteFunc(void *buffer, size_t size, size_t nmemb, void *userp)
Definition: retriever.cpp:87
string package
static CURLStaticInit g_curl_init
Definition: retriever.cpp:67
boost::shared_array< uint8_t > data
Definition: retriever.h:58
A combination of a pointer to data in memory along with the data&#39;s size.
Definition: retriever.h:52
ROSLIB_DECL std::string getPath(const std::string &package_name)
MemoryResource get(const std::string &url)
Get a file and store it in memory.
Definition: retriever.cpp:98
#define ROS_ERROR(...)
std::vector< uint8_t > v
Definition: retriever.cpp:84


resource_retriever
Author(s): Josh Faust , Ioan Sucan
autogenerated on Fri Apr 5 2019 02:08:43