� P:}Pc@s�dZdZdZdZdZdZdZddlZddlZddl Z dd l m Z ddl Z ddl Z dd l mZmZmZmZddlZdd lmZdd lmZdd lmZe je�Zdefd��YZdefd��YZddd�Z ddd�Z!ddd�Z"ddd�Z#dd�Z$d�Z%de&fd��YZ'd�Z(edkr�e j)�e(�ndS(s`Utilities using NDG HTTPS Client, including a main module that can be used to fetch from a URL. s R B Wilkinsons09/12/11s2(C) 2011 Science and Technology Facilities Councils-BSD - see LICENSE file in top-level directorysPhilip.Kershaw@stfc.ac.uks$Id$i����N(t OptionParser(t HTTPHandlertHTTPCookieProcessortHTTPBasicAuthHandlertHTTPPasswordMgrWithDefaultRealm(t build_opener(tHTTPSContextHandler(tssl_context_utiltAccumulatingHTTPCookieProcessorcBseZdZd�ZeZRS(stCookie processor that adds new cookies (instead of replacing the existing ones as HTTPCookieProcessor does) cCs�d}tj|j�|ji|j|j�}|jj|�|j|�}|r�|j |�r�|j|�}dj ||g�}|j ||�q�|j ||�n|S(s�Processes cookies for a HTTP request. @param request: request to process @type request: urllib2.Request @return: request @rtype: urllib2.Request tCookies; ( turllib2tRequestt get_full_urltdatatorigin_req_hostt unverifiablet cookiejartadd_cookie_headert get_headert has_headertjointadd_unredirected_header(tselftrequesttCOOKIE_HEADER_NAMEt tmp_requestt new_cookiest old_cookiestmerged_cookies((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pyt http_request s   (t__name__t __module__t__doc__Rt https_request(((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pyRs t URLFetchErrorcBseZdZRS(sError fetching content from URL(RRR (((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pyR">scCsct||d|d|�\}}}|rS|tjkrS|j�}|j�|St|��dS(s�Returns data retrieved from a URL. @param url: URL to attempt to open @type url: basestring @param config: SSL context configuration @type config: Configuration @return data retrieved from URL or None R thandlersN(topen_urlthttplibtOKtreadtcloseR"(turltconfigR R#t return_codetreturn_messagetresponset return_data((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pytfetch_from_urlBs  c Cs�t||d|d|�\}}}|tjkrr|j�}|j�t|d�} | j|�| j�n|||tjkfS(s�Writes data retrieved from a URL to a file. @param url: URL to attempt to open @type url: basestring @param config: SSL context configuration @type config: Configuration @param output_file: output file @type output_file: basestring @return: tuple ( returned HTTP status code or 0 if an error occurred returned message boolean indicating whether access was successful) R R#tw(R$R%R&R'R(topentwrite( R)R*t output_fileR R#R+R,R-R.toutfile((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pytfetch_from_url_to_fileSs     cCsMt||d|d|�\}}}|r=|tjkr=|St|��dS(sReturns data retrieved from a URL. @param url: URL to attempt to open @type url: basestring @param config: SSL context configuration @type config: Configuration @return: data retrieved from URL or None @rtype: file derived type R R#N(R$R%R&R"(R)R*R R#R+R,R-((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pytfetch_stream_from_urljs c Cs�|jrdnd}|jr*|j}n tj�}t|�}|sQg}n|j|�|jr�td|�}t|jd|�}|j ||g�n|j r�t t ��} | j ddd|d|jdd|jd�|j| �nt||j�s1|jtji��tjd�n9|jrj|jtj|j��tjd |j�ntd |j|�} |j} | dkr�i} ntj||| �} d} d }d}yj| j| �}|j}|j} tjtj�r-x0t|�D]\}}tjd ||�qWnWn�tj k r�}|j} d |j}tjtj�r�tjd|j|j�q�nWt!k r�}d |j"�}tjtj�r�ddl#}tj|j$��q�nX| ||fS(s�Attempts to open a connection to a specified URL. @param url: URL to attempt to open @param config: SSL context configuration @type config: Configuration @param data: HTTP POST data @type data: str @param handlers: list of custom urllib2 handlers to add to the request @type handlers: iterable @return: tuple ( returned HTTP status code or 0 if an error occurred returned message or error description response object) iit debugleveltrealmturitusertpasswdsNot using proxysConfiguring proxies: %st ssl_contextts %s : %ss Error: %ss%s %si����N(%tdebugtcookiet cookielibt CookieJarRtappendRRR<textendthttp_basicauthRRt add_passwordtNonethttpautht_should_use_proxytno_proxyR t ProxyHandlertlogtproxiesRtheadersR R1tmsgtcodet isEnabledFortloggingtDEBUGt enumeratet HTTPErrort Exceptiont__str__t tracebackt format_exc(R)R*R R#R7tcjtcookie_handlert http_handlert https_handlert auth_handlertopenerRMRR+R,R-tindexR?texcRW((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pyR${sf                    cCs�|dkr$tjjdd�}n|}tjt|��}xCg|jd�D]}|j�^qRD]}|j|krht SqhWt S(s�Determines whether a proxy should be used to open a connection to the specified URL, based on the value of the no_proxy environment variable. @param url: URL @type url: basestring or urllib2.Request RIR=t,N( RFtostenvirontgetturlparset_url_as_stringtsplittstripthostnametFalsetTrue(R)RItno_proxy_effectiveturlObjthtnp((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pyRH�s /cCsLt|tj�r|j�St|t�r/|Stdttjf��dS(s�Returns the URL string from a URL value that is either a string or urllib2.Request.. @param url: URL @type url: basestring or urllib2.Request @return: URL string @rtype: basestring sExpected type %r or %rN(t isinstanceR R R t basestringt TypeError(R)((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pyRf�s  t ConfigurationcBs)eZdZedddddd�ZRS(sConnection configuration. cCsC||_||_||_||_||_||_||_dS(s� @param ssl_context: SSL context to use with this configuration @type ssl_context: OpenSSL.SSL.Context @param debug: if True, output debugging information @type debug: bool @param proxies: proxies to use for @type proxies: dict with basestring keys and values @param no_proxy: hosts for which a proxy should not be used @type no_proxy: basestring @param cookie: cookies to set for request @type cookie: cookielib.CookieJar @param http_basicauth: http authentication, or None @type http_basicauth: tuple of (username,password) @param headers: http headers @type headers: dict N(R<R>RLRIR?RDRM(RR<R>RLRIR?RDRM((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pyt__init__�s      N(RRR RjRFRt(((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pyRs�s c Cs�tdd�}|jddddddd tjjd �d d �|jd dddddd d8d d�|jddddddd d8d d�|jddddddd td d�|jddddddd d8d d�|jd d!dd"ddd d8d d#�|jd$d%dddd&d td d'�|jd(d)dd*dd+d d8d d,�|jd-dd.dd/dd0d d1�|j�\}}t|�d2kr�|j d3�n|d4}|j r�t j �j t j�n|jr tjj|j�r |j}nd8}|jr:tjj|j�r:|j}nd8}|jrjtjj|j�rj|j}nd8}|j }|jr�tjj|j�r�t|j�}|j�} |j�nd8} |jr�|jjd5d2�} nd8} i} |jrAxB|jD]4} | jd5d2�\} }|j�| | j�<qWntj||d8|||�}t ||j d6| d/| �}|j!r�t"|||j!| �d7 \}}t#||��nt$||�} | GHd8S(9sHUtility to fetch data using HTTP or HTTPS GET from a specified URL. tusages%prog [options] urls-cs --certificatetdestt cert_filetmetavartFILEtdefaults~/credentials.pemthelps4Certificate file - defaults to $HOME/credentials.pems-ks --private-keytkey_files3Private key file - defaults to the certificate files-ts--ca-certificate-dirtca_dirtPATHs%Trusted CA certificate file directorys-ds--debugtactiont store_trueR>sPrint debug information.s-ps--post-data-filet data_filesPOST data files-fs--fetchR3s Output files-ns--no-verify-peertno_verify_peers&Skip verification of peer certificate.s-as --basicauthtauths USER:PASSWDsHTTP authentication credentialss--headerRBRMs HEADER: VALUEsAdd HTTP header to requestisIncorrect number of argumentsit:RDiN(%Rt add_optionRbtpatht expanduserRFRjt parse_argstlenterrorR>RQt getLoggertsetLevelRRR|texistsRwR}R�R�R1R'R(t basicauthR�RgRMtlstripRhRtmake_ssl_contextRsR3R5t SystemExitR/(tparsertoptionstargsR)R|RwR}t verify_peerR�R RDRMRntkeytvalR<R*R+R,((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pytmains�                  t__main__(*R t __author__t__date__t __copyright__t __license__t __contact__t __revision__R@R%RQtoptparseRRbR RRRRRet$ndg.httpsclient.urllib2_build_openerRtndg.httpsclient.httpsRtndg.httpsclientRR�RRKRRUR"RFR/R5R6R$RHRftobjectRsR�t basicConfig(((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pyt<module>s>     " "Z   d