File: upload.py

package info (click to toggle)
mongrel2 1.12.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 6,020 kB
  • sloc: ansic: 39,099; python: 2,833; sql: 1,555; javascript: 1,202; sh: 467; makefile: 360; asm: 189; yacc: 145; php: 73; awk: 28; sed: 5
file content (49 lines) | stat: -rw-r--r-- 1,431 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from mongrel2 import handler
try:
    import json
except:
    import simplejson as json

import hashlib

sender_id = "82209006-86FF-4982-B5EA-D1E29E55D481"

conn = handler.Connection(sender_id, "tcp://127.0.0.1:9997",
                          "tcp://127.0.0.1:9996")
while True:
    print "WAITING FOR REQUEST"

    req = conn.recv()

    if req.is_disconnect():
        print "DISCONNECT"
        continue

    elif req.headers.get('x-mongrel2-upload-done', None):
        expected = req.headers.get('x-mongrel2-upload-start', "BAD")
        upload = req.headers.get('x-mongrel2-upload-done', None)

        if expected != upload:
            print "GOT THE WRONG TARGET FILE: ", expected, upload
            continue

        body = open(upload, 'r').read()
        print "UPLOAD DONE: BODY IS %d long, content length is %s" % (
            len(body), req.headers['content-length'])

        response = "UPLOAD GOOD: %s" % hashlib.md5(body).hexdigest()

    elif req.headers.get('x-mongrel2-upload-start', None):
        print "UPLOAD starting, don't reply yet."
        print "Will read file from %s." % req.headers.get('x-mongrel2-upload-start', None)
        continue

    else:
        response = "<pre>\nSENDER: %r\nIDENT:%r\nPATH: %r\nHEADERS:%r\nBODY:%r</pre>" % (
            req.sender, req.conn_id, req.path, 
            json.dumps(req.headers), req.body)

        print response

    conn.reply_http(req, response)