libdap  Updated for version 3.17.0
HTTPConnect.h
00001 
00002 // -*- mode: c++; c-basic-offset:4 -*-
00003 
00004 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
00005 // Access Protocol.
00006 
00007 // Copyright (c) 2002,2003 OPeNDAP, Inc.
00008 // Author: James Gallagher <jgallagher@opendap.org>
00009 //
00010 // This library is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU Lesser General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2.1 of the License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00023 //
00024 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
00025 
00026 #ifndef _httpconnect_h
00027 #define _httpconnect_h
00028 
00029 
00030 #include <string>
00031 
00032 #include <curl/curl.h>
00033 //No longer used in CURL - pwest April 09, 2012
00034 //#include <curl/types.h>
00035 #include <curl/easy.h>
00036 
00037 #ifndef _rc_reader_h_
00038 #include "RCReader.h"
00039 #endif
00040 
00041 #ifndef _object_type_h
00042 #include "ObjectType.h"
00043 #endif
00044 
00045 #ifndef _http_cache_h
00046 #include "HTTPCache.h"
00047 #endif
00048 
00049 #ifndef http_response_h
00050 #include "HTTPResponse.h"
00051 #endif
00052 
00053 #ifndef _util_h
00054 #include "util.h"
00055 #endif
00056 
00057 using std::string;
00058 using std::vector;
00059 
00060 namespace libdap
00061 {
00062 
00063 extern int www_trace;
00064 extern int dods_keep_temps;
00065 
00072 class HTTPConnect
00073 {
00074 private:
00075     CURL *d_curl;
00076     RCReader *d_rcr;
00077     HTTPCache *d_http_cache;
00078 
00079     char d_error_buffer[CURL_ERROR_SIZE]; // A human-readable message.
00080     std::string d_content_type; // apparently read by libcurl; this is valid only after curl_easy_perform()
00081 
00082     bool d_accept_deflate;
00083 
00084     string d_username;  // extracted from URL
00085     string d_password;  // extracted from URL
00086     string d_upstring;  // used to pass info into curl
00087 
00088     string d_cookie_jar;
00089 
00090     vector<string> d_request_headers; // Request headers
00091 
00092     int d_dap_client_protocol_major;
00093     int d_dap_client_protocol_minor;
00094 
00095     bool d_use_cpp_streams;     // Build HTTPResponse objects using fstream and not FILE*
00096 
00097     void www_lib_init();
00098     long read_url(const string &url, FILE *stream, vector<string> *resp_hdrs,
00099                   const vector<string> *headers = 0);
00100 
00101     HTTPResponse *plain_fetch_url(const string &url);
00102     HTTPResponse *caching_fetch_url(const string &url);
00103 
00104     bool url_uses_proxy_for(const string &url);
00105     bool url_uses_no_proxy_for(const string &url) throw();
00106 
00107     void extract_auth_info(string &url);
00108 
00109     friend size_t save_raw_http_header(void *ptr, size_t size, size_t nmemb,
00110                                        void *http_connect);
00111     friend class HTTPConnectTest;
00112     friend class ParseHeader;
00113 
00114 protected:
00120     HTTPConnect();
00121         HTTPConnect(const HTTPConnect &);
00122         HTTPConnect &operator=(const HTTPConnect &);
00124 
00125 public:
00126     HTTPConnect(RCReader *rcr, bool use_cpp = false);
00127 
00128     virtual ~HTTPConnect();
00129 
00130     void set_credentials(const string &u, const string &p);
00131     void set_accept_deflate(bool defalte);
00132     void set_xdap_protocol(int major, int minor);
00133 
00134     bool use_cpp_streams() const { return d_use_cpp_streams; }
00135     void set_use_cpp_streams(bool use_cpp_streams) { d_use_cpp_streams = use_cpp_streams; }
00136 
00143     void set_cookie_jar(const string &cookie_jar) { d_cookie_jar = cookie_jar; }
00144 
00150     void set_cache_enabled(bool enabled) {
00151         if (d_http_cache)
00152             d_http_cache->set_cache_enabled(enabled);
00153     }
00154 
00156     bool is_cache_enabled() { return (d_http_cache) ? d_http_cache->is_cache_enabled() : false; }
00157 
00158     HTTPResponse *fetch_url(const string &url);
00159 };
00160 
00161 } // namespace libdap
00162 
00163 #endif // _httpconnect_h