def generateTestFile(minSize): fname = tempfile.mktemp() f = open(fname, 'w') data = generateRandom(minSize/50) for i in range(0, 51+random.randrange(50)): f.write(data) f.close() filename = os.path.join("/tmp",fname) os.rename(fname,filename) return filename
def generateFiles(minsize): fname = tempfile.mktemp() f = open(fname, 'w') f.write('\0'*minsize) f.write(generateRandom(random.randrange(256)+1)) f.close() filekey = hashfile(fname) filekey = fencode(int(filekey, 16)) filename = os.path.join("/tmp",filekey) os.rename(fname,filename) filenamebad = os.path.join("/tmp/","bad"+filekey[3:]) shutil.copy(filename, filenamebad) return (filekey, filename, filenamebad)
def sendChallenge(request, reqKu, id): challenge = generateRandom(challengelength) while challenge[0] == '\x00': # make sure we have at least challengelength bytes challenge = generateRandom(challengelength) addChallenge(challenge) loggerauth.debug("unencrypted challenge is %s" % fencode(binascii.unhexlify(id)+challenge)) echallenge = reqKu.encrypt(binascii.unhexlify(id)+challenge)[0] echallenge = fencode(echallenge) loggerauth.debug("echallenge = %s" % echallenge) # since challenges will result in a new req/resp pair being generated, # these could take much longer than the primitive_to. Expire in # 15*primitive_to reactor.callLater(primitive_to*15, expireChallenge, challenge, True) resp = 'challenge = %s' % echallenge loggerauth.debug("resp = %s" % resp) request.setResponseCode(http.UNAUTHORIZED, echallenge) request.setHeader('Connection', 'close') request.setHeader('WWW-Authenticate', 'Basic realm="%s"' % 'default') request.setHeader('Content-Length', str(len(resp))) request.setHeader('Content-Type', 'text/html') request.setHeader('Pragma','claimreserve=5555') # XXX: this doesn't work return resp
def fileUpload(host, port, selector, files, form=(), headers={}): """ Performs a file upload via http. host - webserver hostname port - webserver listen port selector - the request (relative URL) files - list of files to upload. list contains tuples, with the first entry as filename/file-like obj and the second as form element name. If the first element is a file-like obj, the element will be used as the filename. If the first element is a filename, the filename's basename will be used as the filename on the form. Type will be "application/octet-stream" form (optional) - a list of pairs of additional name/value form elements (param/values). [hopefully, this method goes away in twisted-web2] """ # XXX: set timeout (based on filesize?) port = int(port) rand_bound = binascii.hexlify(generateRandom(13)) boundary = "---------------------------"+rand_bound CRLF = '\r\n' body_content_type = "application/octet-stream" content_type = "multipart/form-data; boundary="+boundary content_length = 0 H = [] for (param, value) in form: H.append('--' + boundary) H.append('Content-Disposition: form-data; name="%s"' % param) H.append('') H.append('%s' % value) form_data = CRLF.join(H)+CRLF content_length = content_length + len(form_data) fuploads = [] for file, element in files: if file == None: file = "/dev/null" # XXX: not portable if 'read' in dir(file): fname = element file.seek(0,2) file_length = file.tell() file.seek(0,0) else: fname = os.path.basename(file) file_length = os.stat(file)[stat.ST_SIZE] #logger.info("upload file %s len is %d" % (fname, file_length)) H = [] # stuff that goes above file data T = [] # stuff that goes below file data H.append('--' + boundary) H.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (element, fname)) H.append('Content-Type: %s\n' % body_content_type) H.append('') file_headers = CRLF.join(H) content_length = content_length + len(file_headers) + file_length fuploads.append((file_headers, file, file_length)) T.append('--'+boundary+'--') T.append('') T.append('') trailer = CRLF.join(T) content_length = content_length + len(trailer) h = httplib.HTTPConnection(host, port) # XXX: blocking h.putrequest('POST', selector) for pageheader in headers: h.putheader(pageheader, headers[pageheader]) h.putheader('Content-Type', content_type) h.putheader('Content-Length', content_length) h.endheaders() h.send(form_data) for fheader, file, flen in fuploads: if 'read' not in dir(file): file = open(file, 'r') h.send(fheader) h.send(file.read(flen)+CRLF) # XXX: blocking file.close() h.send(trailer) return h
def sendChallenge(self): self.challenge = fencode(generateRandom( ServerPrimitives.challengelength)) echallenge = self.config.Ku.encrypt(self.challenge)[0] echallenge = fencode(echallenge) return echallenge
def _sendFile(self, request, filekey, reqKu, returnMeta): fname = os.path.join(self.config.storedir,filekey) loggerretr.debug("reading file data from %s" % fname) # XXX: make sure requestor owns the file? if returnMeta: loggerretr.debug("returnMeta = %s" % returnMeta) request.setHeader('Content-type', 'Multipart/Related') rand_bound = binascii.hexlify(generateRandom(13)) request.setHeader('boundary', rand_bound) if not os.path.exists(fname): # check for tarball for originator tarball = os.path.join(self.config.storedir,reqKu.id()+".tar") tarballs = [] if os.path.exists(tarball+'.gz'): tarballs.append((tarball+'.gz', 'r:gz')) if os.path.exists(tarball): tarballs.append((tarball, 'r')) loggerretr.debug("tarballs = %s" % tarballs) # XXX: does this work? does it close both tarballs if both got # opened? for tarball, openmode in tarballs: tar = tarfile.open(tarball, openmode) try: tinfo = tar.getmember(filekey) returnedMeta = False if returnMeta: loggerretr.debug("tar returnMeta %s" % filekey) try: metas = [f for f in tar.getnames() if f[:len(filekey)] == filekey and f[-4:] == 'meta'] loggerretr.debug("tar returnMetas=%s" % metas) for m in metas: minfo = tar.getmember(m) H = [] H.append("--%s" % rand_bound) H.append("Content-Type: " "Application/octet-stream") H.append("Content-ID: %s" % m) H.append("Content-Length: %d" % minfo.size) H.append("") H = '\r\n'.join(H) request.write(H) request.write('\r\n') tarm = tar.extractfile(minfo) loggerretr.debug("successful metadata" " RETRIEVE (from %s)" % tarball) # XXX: bad blocking stuff while 1: buf = tarm.read() if buf == "": break request.write(buf) request.write('\r\n') tarm.close() H = [] H.append("--%s" % rand_bound) H.append("Content-Type: Application/octet-stream") H.append("Content-ID: %s" % filekey) H.append("Content-Length: %d" % tinfo.size) H.append("") H = '\r\n'.join(H) request.write(H) request.write('\r\n') returnedMeta = True except: # couldn't find any metadata, just return normal # file loggerretr.debug("no metadata found") pass # XXX: bad blocking stuff tarf = tar.extractfile(tinfo) # XXX: update timestamp on tarf in tarball loggerretr.debug("successful RETRIEVE (from %s)" % tarball) # XXX: bad blocking stuff while 1: buf = tarf.read() if buf == "": break request.write(buf) tarf.close() tar.close() if returnedMeta: T = [] T.append("") T.append("--%s--" % rand_bound) T.append("") T = '\r\n'.join(T) request.write(T) return "" except: tar.close() request.setResponseCode(http.NOT_FOUND, "Not found: %s" % filekey) request.write("Not found: %s" % filekey) else: f = BlockFile.open(fname,"rb") loggerretr.log(logging.INFO, "successful RETRIEVE for %s" % filekey) meta = f.meta(int(reqKu.id(),16)) if returnMeta and meta: loggerretr.debug("returnMeta %s" % filekey) loggerretr.debug("returnMetas=%s" % meta) for m in meta: H = [] H.append("--%s" % rand_bound) H.append("Content-Type: Application/octet-stream") H.append("Content-ID: %s.%s.meta" % (filekey, m)) H.append("Content-Length: %d" % len(meta[m])) H.append("") H.append(meta[m]) H = '\r\n'.join(H) request.write(H) request.write('\r\n') H = [] H.append("--%s" % rand_bound) H.append("Content-Type: Application/octet-stream") H.append("Content-ID: %s" % filekey) H.append("Content-Length: %d" % f.size()) H.append("") H = '\r\n'.join(H) request.write(H) request.write('\r\n') # XXX: bad blocking stuff while 1: buf = f.read() if buf == "": break request.write(buf) f.close() if returnMeta and meta: T = [] T.append("") T.append("--%s--" % rand_bound) T.append("") request.write('\r\n'.join(T)) return ""