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 #include <regex>
38 
39 namespace resource_retriever
40 {
41 
43 {
44 public:
46  : initialized_(false)
47  {
48  CURLcode ret = curl_global_init(CURL_GLOBAL_ALL);
49  if (ret != 0)
50  {
51  ROS_ERROR("Error initializing libcurl! retcode = %d", ret);
52  }
53  else
54  {
55  initialized_ = true;
56  }
57  }
58 
60  {
61  if (initialized_)
62  {
63  curl_global_cleanup();
64  }
65  }
66 
68 };
70 
72 {
73  curl_handle_ = curl_easy_init();
74 }
75 
77 {
78  if (curl_handle_)
79  {
80  curl_easy_cleanup(curl_handle_);
81  }
82 }
83 
85 {
86  std::vector<uint8_t> v;
87 };
88 
89 size_t curlWriteFunc(void* buffer, size_t size, size_t nmemb, void* userp)
90 {
91  MemoryBuffer* membuf = (MemoryBuffer*)userp;
92 
93  size_t prev_size = membuf->v.size();
94  membuf->v.resize(prev_size + size * nmemb);
95  memcpy(&membuf->v[prev_size], buffer, size * nmemb);
96 
97  return size * nmemb;
98 }
99 
100 MemoryResource Retriever::get(const std::string& url)
101 {
102  std::string mod_url = url;
103  if (url.find("package://") == 0)
104  {
105  mod_url.erase(0, strlen("package://"));
106  size_t pos = mod_url.find("/");
107  if (pos == std::string::npos)
108  {
109  throw Exception(url, "Could not parse package:// format into file:// format");
110  }
111 
112  std::string package = mod_url.substr(0, pos);
113  mod_url.erase(0, pos);
114  std::string package_path = ros::package::getPath(package);
115 
116  if (package_path.empty())
117  {
118  throw Exception(url, "Package [" + package + "] does not exist");
119  }
120 
121  mod_url = "file://" + package_path + mod_url;
122  }
123 
124  //newer versions of curl do not accept spaces in URLs
125  mod_url = std::regex_replace(mod_url, std::regex(" "), "%20");
126 
127  curl_easy_setopt(curl_handle_, CURLOPT_URL, mod_url.c_str());
128  curl_easy_setopt(curl_handle_, CURLOPT_WRITEFUNCTION, curlWriteFunc);
129 
130  char error_buffer[CURL_ERROR_SIZE];
131  curl_easy_setopt(curl_handle_, CURLOPT_ERRORBUFFER , error_buffer);
132 
133  MemoryResource res;
134  MemoryBuffer buf;
135  curl_easy_setopt(curl_handle_, CURLOPT_WRITEDATA, &buf);
136 
137  CURLcode ret = curl_easy_perform(curl_handle_);
138  if (ret != 0)
139  {
140  throw Exception(mod_url, error_buffer);
141  }
142  else if (!buf.v.empty())
143  {
144  res.size = buf.v.size();
145  res.data.reset(new uint8_t[res.size+1]);
146  memcpy(res.data.get(), &buf.v[0], res.size);
147  res.data[res.size] = '\0'; // write trailing zero for interpretation as char*
148  }
149 
150  return res;
151 }
152 
153 }
resource_retriever::CURLStaticInit::CURLStaticInit
CURLStaticInit()
Definition: retriever.cpp:45
resource_retriever::MemoryResource::size
uint32_t size
Definition: retriever.h:59
ros::package::getPath
ROSLIB_DECL std::string getPath(const std::string &package_name)
resource_retriever::g_curl_init
static CURLStaticInit g_curl_init
Definition: retriever.cpp:69
resource_retriever
Definition: retriever.h:38
resource_retriever::CURLStaticInit::~CURLStaticInit
~CURLStaticInit()
Definition: retriever.cpp:59
resource_retriever::MemoryResource
A combination of a pointer to data in memory along with the data's size.
Definition: retriever.h:52
resource_retriever::Retriever::Retriever
Retriever()
Definition: retriever.cpp:71
resource_retriever::MemoryBuffer
Definition: retriever.cpp:84
resource_retriever::MemoryResource::data
boost::shared_array< uint8_t > data
Definition: retriever.h:58
package
string package
console.h
resource_retriever::Retriever::~Retriever
~Retriever()
Definition: retriever.cpp:76
resource_retriever::Retriever::get
MemoryResource get(const std::string &url)
Get a file and store it in memory.
Definition: retriever.cpp:100
retriever.h
resource_retriever::CURLStaticInit
Definition: retriever.cpp:42
package.h
resource_retriever::Retriever::curl_handle_
CURL * curl_handle_
Definition: retriever.h:83
ROS_ERROR
#define ROS_ERROR(...)
resource_retriever::MemoryBuffer::v
std::vector< uint8_t > v
Definition: retriever.cpp:86
resource_retriever::Exception
Definition: retriever.h:41
resource_retriever::curlWriteFunc
size_t curlWriteFunc(void *buffer, size_t size, size_t nmemb, void *userp)
Definition: retriever.cpp:89
resource_retriever::CURLStaticInit::initialized_
bool initialized_
Definition: retriever.cpp:67


resource_retriever
Author(s): Josh Faust , Ioan Sucan
autogenerated on Sun Apr 14 2024 02:35:33