libdap++ Updated for version 3.8.2
HTTPConnect.h
Go to the documentation of this file.
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 #include <curl/easy.h>
00034 
00035 #ifndef _rc_reader_h_
00036 #include "RCReader.h"
00037 #endif
00038 
00039 #ifndef _object_type_h
00040 #include "ObjectType.h"
00041 #endif
00042 
00043 #ifndef _http_cache_h
00044 #include "HTTPCache.h"
00045 #endif
00046 
00047 #ifndef http_response_h
00048 #include "HTTPResponse.h"
00049 #endif
00050 
00051 #ifndef _util_h
00052 #include "util.h"
00053 #endif
00054 
00055 using std::string;
00056 using std::vector;
00057 
00058 namespace libdap
00059 {
00060 
00061 extern int www_trace;
00062 extern int dods_keep_temps;
00063 
00076 class HTTPConnect
00077 {
00078 private:
00079     CURL *d_curl;
00080     RCReader *d_rcr;
00081     HTTPCache *d_http_cache;
00082 
00083     char d_error_buffer[CURL_ERROR_SIZE]; // A human-readable message.
00084 
00085     bool d_accept_deflate;
00086 
00087     string d_username;  // extracted from URL
00088     string d_password;  // extracted from URL
00089     string d_upstring;  // used to pass info into curl
00090 
00091     string d_cookie_jar;
00092 
00093     vector<string> d_request_headers; // Request headers
00094 
00095     int d_dap_client_protocol_major;
00096     int d_dap_client_protocol_minor;
00097 
00098     void www_lib_init();
00099     long read_url(const string &url, FILE *stream, vector<string> *resp_hdrs,
00100                   const vector<string> *headers = 0);
00101     // string get_temp_file(FILE *&stream) throw(InternalErr);
00102     HTTPResponse *plain_fetch_url(const string &url);
00103     HTTPResponse *caching_fetch_url(const string &url);
00104 
00105     bool url_uses_proxy_for(const string &url) throw();
00106     bool url_uses_no_proxy_for(const string &url) throw();
00107 
00108     void extract_auth_info(string &url);
00109 
00110     friend size_t save_raw_http_header(void *ptr, size_t size, size_t nmemb,
00111                                        void *http_connect);
00112     friend class HTTPConnectTest;
00113     friend class ParseHeader;
00114 
00115 protected:
00121     HTTPConnect() {
00122                 throw InternalErr(__FILE__, __LINE__, "Unimplemented method");
00123         }
00124         HTTPConnect(const HTTPConnect &) {
00125                 throw InternalErr(__FILE__, __LINE__, "Unimplemented method");
00126         }
00127         HTTPConnect &operator=(const HTTPConnect &) {
00128                 throw InternalErr(__FILE__, __LINE__, "Unimplemented assignment");
00129         }
00131 
00132 public:
00133     HTTPConnect(RCReader *rcr);
00134 
00135     virtual ~HTTPConnect();
00136 
00137     void set_credentials(const string &u, const string &p);
00138     void set_accept_deflate(bool defalte);
00139     void set_xdap_protocol(int major, int minor);
00140 
00147     void set_cookie_jar(const string &cookie_jar)
00148     {
00149         d_cookie_jar = cookie_jar;
00150     }
00151 
00157     void set_cache_enabled(bool enabled)
00158     {
00159         if (d_http_cache)
00160             d_http_cache->set_cache_enabled(enabled);
00161     }
00162 
00164     bool is_cache_enabled()
00165     {
00166         return (d_http_cache) ? d_http_cache->is_cache_enabled() : false;
00167     }
00168 
00169     HTTPResponse *fetch_url(const string &url);
00170 };
00171 
00172 } // namespace libdap
00173 
00174 #endif // _httpconnect_h