comparison libinterp/dldfcn/urlwrite.cc @ 15326:81fe6665ed3c

Fix errors with string options passed to older libcurl (bug #36717) * urlwrite.cc: Store persistent string options passed to libcurl in curl_handle_rep.
author Mike Miller <mtmiller@ieee.org>
date Fri, 07 Sep 2012 18:14:14 -0400
parents af54273243fd
children accafe1aee7c
comparison
equal deleted inserted replaced
15325:d9b955926a50 15326:81fe6665ed3c
142 return !ascii; 142 return !ascii;
143 } 143 }
144 144
145 octave_refcount<size_t> count; 145 octave_refcount<size_t> count;
146 std::string host; 146 std::string host;
147 std::string url;
148 std::string userpwd;
147 bool valid; 149 bool valid;
148 bool ascii; 150 bool ascii;
149 mutable CURLcode errnum; 151 mutable CURLcode errnum;
150 152
151 private: 153 private:
180 rep (new curl_handle_rep ()) 182 rep (new curl_handle_rep ())
181 { 183 {
182 rep->host = _host; 184 rep->host = _host;
183 init (user, passwd, std::cin, octave_stdout); 185 init (user, passwd, std::cin, octave_stdout);
184 186
185 std::string url = "ftp://" + _host; 187 rep->url = "ftp://" + _host;
186 setopt (CURLOPT_URL, url.c_str ()); 188 setopt (CURLOPT_URL, rep->url.c_str ());
187 189
188 // Setup the link, with no transfer 190 // Setup the link, with no transfer
189 if (!error_state) 191 if (!error_state)
190 perform (); 192 perform ();
191 } 193 }
356 curl_slist_free_all (slist); 358 curl_slist_free_all (slist);
357 } 359 }
358 360
359 void put (const std::string& file, std::istream& is) const 361 void put (const std::string& file, std::istream& is) const
360 { 362 {
361 std::string url = "ftp://" + rep->host + "/" + file; 363 rep->url = "ftp://" + rep->host + "/" + file;
362 setopt (CURLOPT_URL, url.c_str ()); 364 setopt (CURLOPT_URL, rep->url.c_str ());
363 setopt (CURLOPT_UPLOAD, 1); 365 setopt (CURLOPT_UPLOAD, 1);
364 setopt (CURLOPT_NOBODY, 0); 366 setopt (CURLOPT_NOBODY, 0);
365 set_istream (is); 367 set_istream (is);
366 if (! error_state) 368 if (! error_state)
367 perform (); 369 perform ();
368 set_istream (std::cin); 370 set_istream (std::cin);
369 setopt (CURLOPT_NOBODY, 1); 371 setopt (CURLOPT_NOBODY, 1);
370 setopt (CURLOPT_UPLOAD, 0); 372 setopt (CURLOPT_UPLOAD, 0);
371 url = "ftp://" + rep->host; 373 rep->url = "ftp://" + rep->host;
372 setopt (CURLOPT_URL, url.c_str ()); 374 setopt (CURLOPT_URL, rep->url.c_str ());
373 } 375 }
374 376
375 void get (const std::string& file, std::ostream& os) const 377 void get (const std::string& file, std::ostream& os) const
376 { 378 {
377 std::string url = "ftp://" + rep->host + "/" + file; 379 rep->url = "ftp://" + rep->host + "/" + file;
378 setopt (CURLOPT_URL, url.c_str ()); 380 setopt (CURLOPT_URL, rep->url.c_str ());
379 setopt (CURLOPT_NOBODY, 0); 381 setopt (CURLOPT_NOBODY, 0);
380 set_ostream (os); 382 set_ostream (os);
381 if (! error_state) 383 if (! error_state)
382 perform (); 384 perform ();
383 set_ostream (octave_stdout); 385 set_ostream (octave_stdout);
384 setopt (CURLOPT_NOBODY, 1); 386 setopt (CURLOPT_NOBODY, 1);
385 url = "ftp://" + rep->host; 387 rep->url = "ftp://" + rep->host;
386 setopt (CURLOPT_URL, url.c_str ()); 388 setopt (CURLOPT_URL, rep->url.c_str ());
387 } 389 }
388 390
389 void dir (void) const 391 void dir (void) const
390 { 392 {
391 std::string url = "ftp://" + rep->host + "/"; 393 rep->url = "ftp://" + rep->host + "/";
392 setopt (CURLOPT_URL, url.c_str ()); 394 setopt (CURLOPT_URL, rep->url.c_str ());
393 setopt (CURLOPT_NOBODY, 0); 395 setopt (CURLOPT_NOBODY, 0);
394 if (! error_state) 396 if (! error_state)
395 perform (); 397 perform ();
396 setopt (CURLOPT_NOBODY, 1); 398 setopt (CURLOPT_NOBODY, 1);
397 url = "ftp://" + rep->host; 399 rep->url = "ftp://" + rep->host;
398 setopt (CURLOPT_URL, url.c_str ()); 400 setopt (CURLOPT_URL, rep->url.c_str ());
399 } 401 }
400 402
401 string_vector list (void) const 403 string_vector list (void) const
402 { 404 {
403 std::ostringstream buf; 405 std::ostringstream buf;
404 std::string url = "ftp://" + rep->host + "/"; 406 rep->url = "ftp://" + rep->host + "/";
405 setopt (CURLOPT_WRITEDATA, static_cast<void*> (&buf)); 407 setopt (CURLOPT_WRITEDATA, static_cast<void*> (&buf));
406 setopt (CURLOPT_URL, url.c_str ()); 408 setopt (CURLOPT_URL, rep->url.c_str ());
407 setopt (CURLOPT_DIRLISTONLY, 1); 409 setopt (CURLOPT_DIRLISTONLY, 1);
408 setopt (CURLOPT_NOBODY, 0); 410 setopt (CURLOPT_NOBODY, 0);
409 if (! error_state) 411 if (! error_state)
410 perform (); 412 perform ();
411 setopt (CURLOPT_NOBODY, 1); 413 setopt (CURLOPT_NOBODY, 1);
412 url = "ftp://" + rep->host; 414 rep->url = "ftp://" + rep->host;
413 setopt (CURLOPT_WRITEDATA, static_cast<void*> (&octave_stdout)); 415 setopt (CURLOPT_WRITEDATA, static_cast<void*> (&octave_stdout));
414 setopt (CURLOPT_DIRLISTONLY, 0); 416 setopt (CURLOPT_DIRLISTONLY, 0);
415 setopt (CURLOPT_URL, url.c_str ()); 417 setopt (CURLOPT_URL, rep->url.c_str ());
416 418
417 // Count number of directory entries 419 // Count number of directory entries
418 std::string str = buf.str (); 420 std::string str = buf.str ();
419 octave_idx_type n = 0; 421 octave_idx_type n = 0;
420 size_t pos = 0; 422 size_t pos = 0;
443 void get_fileinfo (const std::string& filename, double& filesize, 445 void get_fileinfo (const std::string& filename, double& filesize,
444 time_t& filetime, bool& fileisdir) const 446 time_t& filetime, bool& fileisdir) const
445 { 447 {
446 std::string path = pwd (); 448 std::string path = pwd ();
447 449
448 std::string url = "ftp://" + rep->host + "/" + path + "/" + filename; 450 rep->url = "ftp://" + rep->host + "/" + path + "/" + filename;
449 setopt (CURLOPT_URL, url.c_str ()); 451 setopt (CURLOPT_URL, rep->url.c_str ());
450 setopt (CURLOPT_FILETIME, 1); 452 setopt (CURLOPT_FILETIME, 1);
451 setopt (CURLOPT_HEADERFUNCTION, throw_away); 453 setopt (CURLOPT_HEADERFUNCTION, throw_away);
452 setopt (CURLOPT_WRITEFUNCTION, throw_away); 454 setopt (CURLOPT_WRITEFUNCTION, throw_away);
453 455
454 // FIXME 456 // FIXME
477 } 479 }
478 480
479 setopt (CURLOPT_WRITEFUNCTION, write_data); 481 setopt (CURLOPT_WRITEFUNCTION, write_data);
480 setopt (CURLOPT_HEADERFUNCTION, 0); 482 setopt (CURLOPT_HEADERFUNCTION, 0);
481 setopt (CURLOPT_FILETIME, 0); 483 setopt (CURLOPT_FILETIME, 0);
482 url = "ftp://" + rep->host; 484 rep->url = "ftp://" + rep->host;
483 setopt (CURLOPT_URL, url.c_str ()); 485 setopt (CURLOPT_URL, rep->url.c_str ());
484 486
485 // The MDTM command seems to reset the path to the root with the 487 // The MDTM command seems to reset the path to the root with the
486 // servers I tested with, so cd again into the correct path. Make 488 // servers I tested with, so cd again into the correct path. Make
487 // the path absolute so that this will work even with servers that 489 // the path absolute so that this will work even with servers that
488 // don't end up in the root after an MDTM command. 490 // don't end up in the root after an MDTM command.
560 { 562 {
561 // No data transfer by default 563 // No data transfer by default
562 setopt (CURLOPT_NOBODY, 1); 564 setopt (CURLOPT_NOBODY, 1);
563 565
564 // Set the username and password 566 // Set the username and password
565 std::string userpwd = user; 567 rep->userpwd = user;
566 if (! passwd.empty ()) 568 if (! passwd.empty ())
567 userpwd += ":" + passwd; 569 rep->userpwd += ":" + passwd;
568 if (! userpwd.empty ()) 570 if (! rep->userpwd.empty ())
569 setopt (CURLOPT_USERPWD, userpwd.c_str ()); 571 setopt (CURLOPT_USERPWD, rep->userpwd.c_str ());
570 572
571 // Define our callback to get called when there's data to be written. 573 // Define our callback to get called when there's data to be written.
572 setopt (CURLOPT_WRITEFUNCTION, write_data); 574 setopt (CURLOPT_WRITEFUNCTION, write_data);
573 575
574 // Set a pointer to our struct to pass to the callback. 576 // Set a pointer to our struct to pass to the callback.