Changeset 3315

Show
Ignore:
Timestamp:
12/23/09 11:48:35 (10 years ago)
Author:
yemuru
Message:

Added a new version of httpretrieve.repy and registerhttpcallback.repy with new functionalities.

Location:
seattle/trunk/seattlelib
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • seattle/trunk/seattlelib/httpretrieve.repy

    r3305 r3315  
    2828 
    2929 
    30 def httpretrieve_open(url, http_query=None, http_post=None, http_header=None, header_timeout=None, content_timeout=None, httpheader_limit=8192, httpcontent_limit=4194304): 
     30def httpretrieve_open(url, http_query=None, http_post=None, http_header=None, header_timeout=30, content_timeout=30, httpheader_limit=8192, httpcontent_limit=4194304): 
    3131  """ 
    3232  <Purpose> 
     
    9999    Returns file like obj which can read the http content from http web server.  
    100100  """ 
    101  
    102   # Pick default values for header_timeout and content_timeout if None given. 
    103   if header_timeout is None: 
    104     header_timeout = 30 
    105   if content_timeout is None: 
    106     content_timeout = 30 
    107101   
    108102  # check if url is valid and get host, path, port and query from the given url 
     
    121115  # send http format request to http web server   
    122116  _httpretrieve_sendhttprequest(sock, httpheader)  
    123  
     117   
    124118  # receive the http header lines in a form of list from the http web server 
    125119  httpheaderlines = _httpretrieve_receive_httpheader(sock, header_timeout, httpheader_limit) 
     
    458452    
    459453    # check if the given post data is valid 
    460     if type(http_post) == dict: 
    461       # change the given http post dictionary into a encoded post data with a key and val   
    462       try:  
    463         http_post = urllib_quote_parameters(http_post) 
    464       except Exception, e: 
    465         raise HttpUserInputError('Error encoding the given http post dictionary ' +  str(http_post) + str(e)) 
    466  
    467     if type(http_post) != str: 
    468       raise HttpUserInputError('The given http_post is not a dictionary or string, given: ' + str(type(http_post))) 
    469  
     454    if not type(http_post) == dict: 
     455      raise HttpUserInputError('The given http_post is not a dictionary, given: ' + str(type(http_post))) 
     456 
     457    # change the given http post dictionary into a encoded post data with a key and val   
     458    try:  
     459      http_post = urllib_quote_parameters(http_post) 
     460    except Exception, e: 
     461      raise HttpUserInputError('Error encoding the given http post dictionary ' +  str(http_post) + str(e)) 
    470462 
    471463 
     
    567559        # raise an exception if the key or value is a empty field 
    568560        raise HttpUserInputError('The given empty http header feild of key or value') 
     561      if key.capitalize() != key: 
     562        raise HttpUserInputError('The given http header key is not capitalized, given: ' + key) 
    569563 
    570564      # add the key and value to the http header field 
    571       clienthttpheader += key + ': ' + val + '\r\n'   
     565      clienthttpheader += key + ' : ' + val + '\r\n'   
    572566 
    573567    # return the string of the http header   
     
    771765       
    772766      # check if the content length is valid and retrun it  
    773       if contentlength <= 0:                 
     767      if contentlength < 0:                 
    774768        raise HttpHeaderFormatError('Http header Content-Length format error: provided content length with invalid number ' + str(contentlength)) 
    775769      else: 
  • seattle/trunk/seattlelib/registerhttpcallback.repy

    r3308 r3315  
    2020 
    2121 
    22  
     22include urllib.repy 
    2323include urlparse.repy   
    2424include sockettimeout.repy 
     
    4242 
    4343     callbackfunc: 
    44             The callbackfunc to be called. it should take one argument for http request dictionary. 
    45             The dictionary keys are as follows: 
     44            The callbackfunc to be called. It can take up to three argument . 
     45            First argument- The http request dictionary keys are as follows: 
    4646              http_command - for the http request method GET or POST 
    4747              http_version - which http version the client  used HTTP\1.1 or HTTP\1.0   
     
    5353              will incude Content-Length as a key and 22 as a value 
    5454 
     55            Second argument - httprequest query; this returns a unencoded dictionary of the query 
     56            Third argument - httprequest posted data; this returns a unencoded dictionary of the posted data 
     57 
    5558            RESTRICTIONS: 
    5659              -> Follow the http_hierarchy_error(HttpStatusCodeError)to raise an exception. eg. raise 
    5760                  HttpError404 for file not found 
    5861              -> To redirect raise HttpError301(url) or HttpError302(url). The url has to be valid.   
    59               -> server only excepts string type to process 
     62              -> server only excepts tuple type of [httpcontent, httpheader] where the httpcontent is a 
     63                  string of the content intended to display. httpheader is a dictionary used only if you have a 
     64                  extra header you want to add on the http response.  
    6065 
    6166     httprequest_limit: 
     
    105110      _registerhttpcallback_receive_httpposted_data(sock, httprequest_dictionary, httppost_limit) 
    106111       
     112      # get dictionary decode from the given query  
     113      httprequest_query = _registerhttpcallback_get_decode_dict(httprequest_dictionary, 'query') 
     114 
     115      # get dictionary decode from the given post  
     116      httprequest_posted_data = _registerhttpcallback_get_decode_dict(httprequest_dictionary, 'posted_data') 
     117 
    107118      # place the call back callbackfunc with dictionary that includes http_command, 
    108119      # http_version, path, query, posted_data, and all the http requests headers 
    109       webpage_content = callbackfunc(httprequest_dictionary) 
     120      webpage_content = callbackfunc(httprequest_dictionary, httprequest_query, httprequest_posted_data) 
    110121         
    111122      # callback callbackfunc excecuted, send the processed dynamic web page data to client(web browser) 
     
    286297  return httprequest_dictionary 
    287298   
     299 
     300 
     301 
     302 
     303def _registerhttpcallback_get_decode_dict(httprequest_dictionary , decode_type): 
     304  # returns a decode dictionary of post or query depending up on the encoded style    
     305 
     306  # get the data from the request dictionary  
     307  data_to_decode = httprequest_dictionary[decode_type] 
     308       
     309  if decode_type == 'posted_data': 
     310    # inorder to check if the post is empty use None    
     311    empty_check = None     
     312  else: 
     313    # inorder to check if the query is empty use empty string    
     314     empty_check = '' 
     315         
     316  if data_to_decode != empty_check: 
     317    try: 
     318      # decode the given data depending up on the style it was encoded   
     319      return urllib_unquote_parameters(data_to_decode) 
     320 
     321    except Exception, e: 
     322      # raise an exception if the fails decoding the given data   
     323      raise HttpUserInputError('Invalid ' + decode_type + ' Raised ' + str(e) + ' on decoding') 
     324 
     325  # if the data is empty return None  
     326  return None     
     327 
     328 
    288329 
    289330 
     
    462503 
    463504 
    464 def _registerhttpcallback_send_httpresponse(sock, webpage_content): 
     505def _registerhttpcallback_send_httpresponse(sock, callbackfunc_val): 
    465506  # sends a response to the client(web browser) with a ok http header and the http web page content 
     507  if not type(callbackfunc_val) == list: 
     508    raise HttpUserInputError('Callback func didnt return list, returned ' + str(type(callbackfunc_val))) 
     509 
     510  try: 
     511    webpage_content = callbackfunc_val[0] 
     512    callbckfunc_httpheader = callbackfunc_val[1] 
     513  except Exception, e: 
     514    raise HttpUserInputError('Callback func returned data failed ' + str(e)) 
     515 
     516  # check the given web page content  
    466517  if not type(webpage_content) == str: 
    467     raise HttpUserInputError('Server failed internally: callback func didnt return str, returned ' + str(type(webpage_content))) 
     518    raise HttpUserInputError('Callback func didnt return str for the content, returned ' + str(type(webpage_content))) 
    468519  if len(webpage_content) == 0: 
    469     raise HttpUserInputError('Server failed internally: callback func didnt return any content') 
    470  
    471   # By default, registerhttpcallback permits only files that are 4,096 kilobytes 
    472   if len(webpage_content) >= 4194304: 
    473     raise HttpUserInputError('Server failed internally: callback class content length exceeded') 
     520    raise HttpUserInputError('Callback func didnt return any content') 
     521   
    474522   
    475523  # build the http ok response header  
    476524  httpheader = 'HTTP/1.0 200 OK\n' 
    477   httpheader += 'Content-Type: text/html\n' 
    478525  httpheader += 'Content-Length: ' + str(len(webpage_content)) + '\n' 
     526  #check if there is a given http header and add it to the response 
     527  httpheader += _registerhttpcallback_parse_callbckfunc_httpheader(callbckfunc_httpheader) 
     528     
    479529  httpheader += 'Server: Seattle Testbed\n\n' 
    480530 
     
    487537    raise HttpConnectionError('server failed to send the http content ' + str(e))   
    488538 
     539 
     540 
     541 
     542 
     543def _registerhttpcallback_parse_callbckfunc_httpheader(callbckfunc_httpheader): 
     544  # builds a http header from the given http callback func list 
     545  if callbckfunc_httpheader == None: 
     546    # if the http header isnt given return a empty string 
     547    return '' 
     548  elif not type(callbckfunc_httpheader) == dict: 
     549    # raise an exception if the http header isnt dictionary 
     550    raise HttpUserInputError('The given http header is not a dictionary, given: ' + str(type(callbckfunc_httpheader))) 
     551  else:  
     552    # take the given key and val from the callbckfunc_httpheader dictionary and add them to the http header with 
     553    # correct http format 
     554    httpheaders = '' 
     555    for key, val in callbckfunc_httpheader.items(): 
     556      # make simple checks on the key and val  
     557      if not type(key) == str: 
     558        raise HttpUserInputError('The callback func given http header key isnt str given ' + str(type(key))) 
     559      if not type(val) == str: 
     560        raise HttpUserInputError('The callback func given http header value isnt str given ' + str( type(val))) 
     561      if key == '' or val == '': 
     562        # raise an exception if the key or value is a empty field 
     563        raise HttpUserInputError('The callback func given empty http header feild of key or value') 
     564      if key.capitalize() != key: 
     565        raise HttpUserInputError('The callback func given http header key is not capitalized, given: ' + key) 
     566 
     567      # add the key and value to the http header field 
     568      httpheaders += key + ' : ' + val + '\n'   
     569 
     570    # return the string of the http header   
     571    return httpheaders 
     572 
     573   
    489574 
    490575