Changeset 5709

Show
Ignore:
Timestamp:
08/27/12 11:27:38 (7 years ago)
Author:
leonwlaw
Message:

Fixes bug with Chrome users not being able to load larger webpages

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • seattle/trunk/repy/apps/allpairsping/allpairsping.repy

    r5499 r5709  
    5151 
    5252 
    53 # Displays a web page with the latency information 
    54 def show_status(srcip,srcport,connobj, ch, mainch):  
    5553 
     54# Generates a HTML page that represents the current status of the program 
     55def generate_status_page(): 
    5656  webpage = "<html><head><title>Latency Information</title></head><body><h1>Latency information from "+getmyip()+' </h1><table border="1">' 
    5757 
     
    7070  webpage = webpage + '</table></html>' 
    7171 
    72   # combine everything into one unit 
    73   data = 'HTTP/1.1 200 OK\nContent-Type: text/html\nContent-Length: '+str(len(webpage))+'\nServer: Seattle Testbed\n\n'+webpage 
     72  return webpage 
    7473 
    75   # send all the data 
    76   sent = 0 
    77   while sent < len(data): 
    78     sent += connobj.send(data[sent:]) 
    7974 
    80   # and we're done, so let's close this connection... 
    81   connobj.close() 
     75 
     76# Displays a web page with the latency information 
     77def handle_http_request(srcip,srcport,connobj, ch, mainch): 
     78  # Get the header 
     79  try: 
     80    total_data = '' 
     81    # We use a separate string to store the received data so that we don't have to 
     82    # re-check the beginning parts of the string multiple times 
     83    new_data = '' 
     84    # The HTTP header ends once we see the char combination '\n\n', which 
     85    # is an empty string. 
     86    while '\n\n' not in new_data: 
     87      # Receive in chunks to avoid reading too much data 
     88      data = connobj.recv(4096) 
     89      # This is because Windows uses \r\n, Linux/Macs use \n. 
     90      # By doing this, we avoid having to program special cases for 
     91      # the different endline formats. 
     92      new_data = data.replace('\r\n', '\n') 
     93      total_data += new_data 
     94 
     95    # keep everything after the first '\n\n' intact, just incase there is 
     96    # more content after the header that we may need 
     97    # Potential Bug?: We ignore the overflow data 
     98    header, overflow = total_data.split('\n\n', 1) 
     99 
     100  except Exception, e: 
     101    if "Socket closed" in str(e): 
     102      # We can't do anything if the socket is closed 
     103      return 
     104    raise 
     105 
     106  # Get the request path, which is inbetween the HTTP action keyword and the 
     107  # HTTP version number. 
     108  # The http action keyword is the first word with no whitespace. 
     109  everything_after_httpaction = header.split(None, 1)[1] 
     110  # Following the path is the HTTP/[VERSION_NUMBER]. 
     111  # We can use that as a delimiter to extract the path. 
     112  request_path = everything_after_httpaction.split(" HTTP/")[0] 
     113 
     114  # Generate the data to send back 
     115  # Don't respond with anything if they have something in the request path. 
     116  # This include favicons. We don't want to generate the webpage needlessly. 
     117  if request_path != '/': 
     118    data = 'HTTP/1.1 404 Not Found\n\n' 
     119  else: 
     120    webpage = generate_status_page() 
     121    # combine everything into one unit for sending 
     122    data = 'HTTP/1.1 200 OK\nContent-Type: text/html\nContent-Length: '+str(len(webpage))+'\nServer: Seattle Testbed\n\n'+webpage 
     123 
     124  # send the response 
     125  try: 
     126    sent = 0 
     127    while sent < len(data): 
     128      sent += connobj.send(data[sent:]) 
     129    # and we're done, so let's close this connection... 
     130    connobj.close() 
     131  except Exception, e: 
     132    if "Socket closed" in str(e): 
     133      # We can't do anything if the socket is closed 
     134      return 
     135    raise 
    82136 
    83137 
     
    111165  # we want to register a function to show a status webpage (TCP port) 
    112166  pageport = int(callargs[0]) 
    113   waitforconn(ip,pageport,show_status)   
     167  waitforconn(ip,pageport,handle_http_request) 
    114168