def parseGET(headers,path): #separate the request params from the path splittedPath = path.split('?') msg = splittedPath[1] parsedDict = urlparse.parse_qs(msg) #Note values here are stored in lists, this is so one can handle many inputs with same name, for now we dont want that as our multipart parsing does not support it params = dict() for k,v in parsedDict.iteritems(): params[k] = v[0] request = ServerRequest(headers,None,params) return request
def handleSinglePart(headers,message): contentLength = long(headers['content-length']) if headers['content-type'] == 'application/x-www-form-urlencoded' or headers['content-type'] == 'application/x-www-form-urlencoded; charset=UTF-8': #TODO generalize msg = message.read(contentLength) parsedDict = urlparse.parse_qs(msg) #Note values here are stored in lists, this is so one can handle many inputs with same name, for now we dont want that as our multipart parsing does not support it params = dict() for k,v in parsedDict.iteritems(): params[k] = v[0] log.log(cpc.util.log.TRACE,'msg is %s'%params) request = ServerRequest(headers,None,params) return request
def delegateMessage(self,headers,messageStream): """Delegate the message to another server. Reads the message we get and requests a response from another client.""" if not (headers.has_key('originating-server-id')): headers['originating-server-id'] = ServerConf().getServerId() length = long(headers['content-length']) tmp = tempfile.TemporaryFile('w+b') tmp.write(messageStream.read(length)) tmp.seek(0) content = mmap.mmap(tmp.fileno(),0,mmap.ACCESS_READ) request = ServerRequest(headers,content) ret=self.sendRequest(request,method="PUT") #strip set-cookie from neighbouring server ret.headers.pop('set-cookie', None) # we can close the files associated with sending forward the request # and focus on the response (that we return) content.close() tmp.close() return ret
def handleMultipart(mainHeaders,msgStream): files = dict() params = dict() BOUNDARY = "--"+HttpMethodParser.extractBoundary(mainHeaders) stopBoundary = BOUNDARY+"--" terminateBoundary = '' msgStream.readline() #has an empty line at start that we want to get rid of while terminateBoundary != stopBoundary: headers = mimetools.Message(msgStream) terminateBoundary = '' log.log(cpc.util.log.TRACE,'multipart headers are %s'%headers.headers) if(ServerRequest.isFile(headers['Content-Disposition'])): file = tempfile.TemporaryFile(mode="w+b") name = ServerRequest.getFieldName(headers['Content-Disposition']) notused,contentDispositionParams = cgi.parse_header(headers['Content-Disposition']) name = contentDispositionParams['name'] #if we have a content length we just read it and store the data contentLength = headers.getheader('Content-Length') if(contentLength): # If a content length is sent we parse the nice way bytes = int(contentLength) if(ServerRequest.isFile(headers['Content-Disposition'])): file.write(msgStream.read(bytes)) else: line = msgStream.read(bytes) log.log(cpc.util.log.TRACE,"line is "+line) params[name] = line msgStream.readline() ## we will have a trailin CRLF that we just want to get rid of if(ServerRequest.isFile(headers['Content-Disposition'])): readBytes = 0 while(True): line = msgStream.readline() if re.search(BOUNDARY,line): #time to wrap it up if(line[-2:] == '\r\n'): line = line[:-2] elif(line[-1:] == '\n'): line = line[:-1] terminateBoundary = line file.seek(0) skipBytes = 2 realFile = tempfile.TemporaryFile(mode="w+b") realFile.write(file.read(readBytes-skipBytes)) file.close() realFile.seek(0) #For testing during dev only!! #runTest(realFile) files[name]= realFile break else: readBytes +=len(line) file.write(line) else: while(True): line = msgStream.readline() if(line[-2:] == '\r\n'): line = line[:-2] elif(line[-1:] == '\n'): line = line[:-1] if re.search(BOUNDARY,line): terminateBoundary = line break; else: if name in params: params[name]+= line else: params[name] = line return ServerRequest(mainHeaders,None,params,files)