Changeset 3332

Show
Ignore:
Timestamp:
01/05/10 15:50:31 (10 years ago)
Author:
yemuru
Message:

Updated httpretrieve.repy and registerhttpcallback.repy

Location:
seattle/trunk/seattlelib
Files:
2 modified

Legend:

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

    r3315 r3332  
    112112  # build a http format request using the given port, host, path and query 
    113113  httpheader = _httpretrieve_buildhttprequest(http_header, port, host, path, url_query, http_query, http_post) 
    114    
     114 
    115115  # send http format request to http web server   
    116116  _httpretrieve_sendhttprequest(sock, httpheader)  
    117    
     117 
    118118  # receive the http header lines in a form of list from the http web server 
    119119  httpheaderlines = _httpretrieve_receive_httpheader(sock, header_timeout, httpheader_limit) 
     
    318318    else:  
    319319    # check if limit is a valid number 
    320       if not type(limit) == int: 
     320      if not type(webpage_content) == int: 
    321321        raise HttpUserInputError('User input Error: given a none int to receive' + str(e))   
    322322      elif limit < 0: 
     
    479479    # there is no posted data, use http GET method    
    480480    httpheader = _httpretrieve_httprequestmain_header('GET', url_query, dict_query, path, host, port) 
    481     # if http header is given, add client headers to the request  
     481    # add client header if given  
    482482    httpheader += _httpretrieve_parse_clienthttpheader(http_header) 
    483483    # add a new line to indicate that the header request is complete 
    484484    httpheader += '\r\n' 
     485 
    485486 
    486487  # return header with a new line which is signal for http header is done  
     
    551552    clienthttpheader = '' 
    552553    for key, val in http_header.items(): 
    553       # make simple checks on the key and val  
    554       if not type(key) == str: 
    555         raise HttpUserInputError('The given http header key isnt str given ' + str(type(key))) 
    556       if not type(val) == str: 
    557         raise HttpUserInputError('The given http header value isnt str given ' + str( type(val))) 
    558       if key == '' or val == '': 
    559         # raise an exception if the key or value is a empty field 
    560         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) 
    563  
    564       # add the key and value to the http header field 
     554      # if the key is not capital letter raise an exception 
    565555      clienthttpheader += key + ' : ' + val + '\r\n'   
    566556 
     
    576566    sock.send(httpheader) 
    577567  except Exception, e: 
    578     if 'Socket closed' not in str(e):  
    579       sock.close() 
     568    sock.close() 
    580569    raise HttpConnectionError('Connection error: on sending http request to server ' + str(e)) 
    581570 
     
    765754       
    766755      # check if the content length is valid and retrun it  
    767       if contentlength < 0:                 
     756      if contentlength <= 0:                 
    768757        raise HttpHeaderFormatError('Http header Content-Length format error: provided content length with invalid number ' + str(contentlength)) 
    769758      else: 
     
    772761  # there wasn't a content-length line or the content length was given but didnt give a int  
    773762  return None 
    774  
    775  
  • seattle/trunk/seattlelib/registerhttpcallback.repy

    r3321 r3332  
    530530  # http header followed by http content and close the connection 
    531531  try: 
    532     sock.send(httpheader)  
    533     sock.send(webpage_content) 
     532    sock.send(httpheader) 
     533    # sends data by chunk of 1024 charactors at a time 
     534    _registerhttpcallback_sendbychunk(webpage_content, 1024, sock) 
    534535    sock.close()  
    535536  except Exception, e: 
    536537    raise HttpConnectionError('server failed to send the http content ' + str(e))   
     538 
     539 
     540 
     541 
     542 
     543def _registerhttpcallback_sendbychunk(full_data, chunck_size, sock): 
     544  # sends a given data to a socket connection by the chunk amount one at a time.  
     545  length_of_data = len(full_data) 
     546  start = 0  
     547  end = chunck_size 
     548  # used to signal when there is no more data left to parse by chunk size 
     549  done = False 
     550   
     551  while not done : 
     552    if length_of_data <= end: 
     553      # last end of data 
     554      end = length_of_data 
     555      done = True 
     556 
     557    # parse the given data by the given chunk size and keep on sending by chunk bytes 
     558    chuncked_data = full_data[start:end] 
     559 
     560    try: 
     561      sock.send(chuncked_data)   
     562    except Exception, e: 
     563      raise Exception('faild on sending data: ' + str(e)) 
     564    else: 
     565      # change start and end to send the next part of the data 
     566      start = end 
     567      end += chunck_size 
    537568 
    538569