Ejemplo n.º 1
0
def handledelete(message, headers, reqheadersdic, resheadersdic, entheadersdic,
                 genheadersdic, num, pathdic, cookie_dict, address):
    f_flag = 0
    flag = 0
    try:
        filename, flag = pathreturn(message[1], flag)
        filename1 = str(pathdic['docpath']) + '/' + filename
        f = open(filename1)
    except:
        loc, code = compressedclean.checkredirect(filename, pathdic)
        if loc != 'N/A' and code != 000:
            resheadersdic['Location'] = loc
            rv = codes.error_3(code, 'confiles/error/' + str(code) + '.html',
                               reqheadersdic, resheadersdic, entheadersdic,
                               genheadersdic, num)
            return rv, str(code)
        #print filename1
        rv = codes.error_4(404, "confiles/error/404.html", reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '404'
    if len(message) > 3:
        codes.parseHeaders("", headers[1:-2], reqheadersdic, '')
        #print reqheadersdic
    #print flag
    if ((reqheadersdic['Host'] == ''
         or reqheadersdic['Host'] != 'localhost:1234') and flag == 0):
        rv = codes.error_4(400, 'confiles/error/400.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '400'
    un, pw, ty = compressedclean.checkprotect(filename, pathdic)
    if un != 'N/A' and pw != 'N/A' and reqheadersdic[
            'Authorization'] == '' and ty == 'protect':
        resheadersdic[
            'WWW-Authenticate'] = 'Basic realm="Access to the protected file", charset="UTF-8"'
        rv = codes.error_4(401, 'confiles/error/401.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '401'
    if un != 'N/A' and pw != 'N/A' and reqheadersdic[
            'Authorization'] != '' and ty == 'protect':
        un1, pw1 = codes.parseHeaders("", "", reqheadersdic, 'auth')
        #print un,pw
        if pw1 != pw:
            rv = codes.error_4(403, 'confiles/error/403.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '403'
        if un != 'N/A' and pw != 'N/A' and reqheadersdic[
                'Proxy-Authorization'] == '' and ty == 'proxy':
            resheadersdic[
                'Proxy-Authenticate'] = 'Basic realm="Access to the protected file", charset="UTF-8"'
            rv = codes.error_4(407, 'confiles/error/407.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '407'

    if un != 'N/A' and pw != 'N/A' and reqheadersdic[
            'Proxy-Authorization'] != '' and ty == 'proxy':
        un1, pw1 = codes.parseHeaders("", "", reqheadersdic, 'pauth')
        #print un,pw
        if pw1 != pw:
            rv = codes.error_4(403, 'confiles/error/403.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '403'

    suc_code = delete_file(filename, reqheadersdic, pathdic)
    if (suc_code == 1):
        rv = codes.success_200(204, filename1, reqheadersdic, resheadersdic,
                               entheadersdic, genheadersdic, '', num,
                               cookie_dict, address)
        return rv, '204'
    else:
        rv = codes.error_4(404, "confiles/error/404.html", reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '404'
Ejemplo n.º 2
0
def handleput(message, headers, reqheadersdic, resheadersdic, entheadersdic,
              genheadersdic, data, pathdic, cookie_dict, address):
    f_flag = 0
    flag = 0
    newfileflag = 0
    num = 0
    try:
        filename, flag = pathreturn(message[1], flag)
        filename1 = str(pathdic['docpath']) + '/' + filename
        f = open(filename1)
    except:
        loc, code = compressedclean.checkredirect(filename, pathdic)
        if loc != 'N/A' and code != 000:
            resheadersdic['Location'] = loc
            if code == 301 or code == 302:
                code = 307
            rv = codes.error_3(code, 'confiles/error/' + str(code) + '.html',
                               reqheadersdic, resheadersdic, entheadersdic,
                               genheadersdic, num)
            return rv, str(code)
        newfileflag = 1
    if len(message) > 3:
        codes.parseHeaders("", headers[1:-2], reqheadersdic, '')
    if ((reqheadersdic['Host'] == ''
         or reqheadersdic['Host'] != 'localhost:1234') and flag == 0):
        rv = codes.error_4(400, 'confiles/error/400.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '400'
    if reqheadersdic['Last-Modified'] != '' and newfileflag == 0:
        dt = os.path.getmtime(filename1)
        date1 = str(datetime.datetime.utcfromtimestamp(dt))
        given_date = str(
            datetime.datetime.strptime(reqheadersdic['Last-Modified'],
                                       '%a, %d %b %Y %H:%M:%S GMT'))
        if date1 > given_date:
            rv = codes.error_4(409, 'confiles/error/409.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '409'
    un, pw, ty = compressedclean.checkprotect(filename, pathdic)
    if un != 'N/A' and pw != 'N/A' and ty == 'protect' and reqheadersdic[
            'Authorization'] == '' and newfileflag == 0:
        resheadersdic[
            'WWW-Authenticate'] = 'Basic realm="Access to the protected file", charset="UTF-8"'
        rv = codes.error_4(401, 'confiles/error/401.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '401'
    if un != 'N/A' and pw != 'N/A' and ty == 'protect' and reqheadersdic[
            'Authorization'] != '' and newfileflag == 0:
        un1, pw1 = codes.parseHeaders("", "", reqheadersdic, 'auth')
        if pw1 != pw:
            rv = codes.error_4(403, 'confiles/error/403.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '403'
    if un != 'N/A' and pw != 'N/A' and ty == 'proxy' and reqheadersdic[
            'Proxy-Authorization'] == '' and newfileflag == 0:
        resheadersdic[
            'Proxy-Authenticate'] = 'Basic realm="Access to the protected file", charset="UTF-8"'
        rv = codes.error_4(407, 'confiles/error/407.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '407'
    if un != 'N/A' and pw != 'N/A' and ty == 'proxy' and reqheadersdic[
            'Proxy-Authorization'] != '' and newfileflag == 0:
        un1, pw1 = codes.parseHeaders("", "", reqheadersdic, 'pauth')
        if pw1 != pw:
            rv = codes.error_4(403, 'confiles/error/403.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '403'
    if newfileflag == 1:
        f = open(filename1, "w+")
        f.write(data)
        f.close()
        mime_type = ""
        if (reqheadersdic['Content-Type'] == ""):
            mime = magic.Magic(mime=True)
            mime_type = mime.from_file(filename1)
        else:
            mime_type = reqheadersdic['Content-Type']
        check_var(filename, reqheadersdic, mime_type, pathdic)
        resheadersdic['Location'] = filename
        rv = codes.success_200(201, filename1, reqheadersdic, resheadersdic,
                               entheadersdic, genheadersdic, '', num,
                               cookie_dict, address)
        return rv, '201'
    if newfileflag == 0:
        if (reqheadersdic['Content-Range'] == ""):
            f = open(filename1, "w")
            f.write(data)
            f.close()
        else:
            f = open(filename1, "r+b")
            ranges = reqheadersdic['Content-Range'].split()
            range_av = ranges[1].split("/")
            range_occ = range_av[0].split("-")
            f.seek(int(range_occ[0].strip()), 0)
            f.write(data.strip("\r\n"))
            f.close()
        mime_type = ""
        if (reqheadersdic['Content-Type'] == ""):
            mime = magic.Magic(mime=True)
            mime_type = mime.from_file(filename1)
        else:
            mime_type = reqheadersdic['Content-Type']
        check_existing_var(filename, reqheadersdic, mime_type)
        rv = codes.success_200(204, filename1, reqheadersdic, resheadersdic,
                               entheadersdic, genheadersdic, '', num,
                               cookie_dict, address)
        return rv, '204'
Ejemplo n.º 3
0
def handleexpect(message, headers, reqheadersdic, resheadersdic, entheadersdic,
                 genheadersdic, pathdic, address):
    f_flag = 0
    flag = 0
    newfileflag = 0
    num = 0
    try:
        filename, flag = pathreturn(message[1], flag)
        filename1 = str(pathdic['docpath']) + '/' + filename
        f = open(filename1)
    except:
        loc, code = compressedclean.checkredirect(filename, pathdic)
        if loc != 'N/A' and code != 000:
            rv = codes.error_4(417, 'confiles/error/417.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return 417, rv
        newfileflag = 1
    if len(message) > 3:
        codes.parseHeaders("", headers[1:-2], reqheadersdic, '')
    if ((reqheadersdic['Host'] == ''
         or reqheadersdic['Host'] != 'localhost:1234') and flag == 0):
        rv = codes.error_4(417, 'confiles/error/417.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return 417, rv
    if reqheadersdic['Last-Modified'] != '' and newfileflag == 0:
        dt = os.path.getmtime(filename1)
        date1 = str(datetime.datetime.utcfromtimestamp(dt))
        given_date = str(
            datetime.datetime.strptime(reqheadersdic['Last-Modified'],
                                       '%a, %d %b %Y %H:%M:%S GMT'))
        if date1 > given_date:
            rv = codes.error_4(417, 'confiles/error/417.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return 417, rv
    un, pw, ty = compressedclean.checkprotect(filename, pathdic)
    if un != 'N/A' and pw != 'N/A' and ty == 'protect' and reqheadersdic[
            'Authorization'] == '' and newfileflag == 0:
        rv = codes.error_4(417, 'confiles/error/417.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return 417, rv
    if un != 'N/A' and pw != 'N/A' and ty == 'protect' and reqheadersdic[
            'Authorization'] != '' and newfileflag == 0:
        un1, pw1 = codes.parseHeaders("", "", reqheadersdic, 'auth')
        #print(pw)
        if pw1 != pw:
            rv = codes.error_4(417, 'confiles/error/417.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return 417, rv
    if un != 'N/A' and pw != 'N/A' and ty == 'proxy' and reqheadersdic[
            'Proxy-Authorization'] == '' and newfileflag == 0:
        rv = codes.error_4(417, 'confiles/error/417.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return 417, rv
    if un != 'N/A' and pw != 'N/A' and ty == 'proxy' and reqheadersdic[
            'Proxy-Authorization'] != '' and newfileflag == 0:
        un, pw = codes.parseHeaders("", "", reqheadersdic, 'pauth')
        if pw != pw:
            rv = codes.error_4(417, 'confiles/error/417.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return 417, rv
    rv = 'HTTP/1.1 100 Continue\n'
    return 100, rv
Ejemplo n.º 4
0
def handleget(message, headers, reqheadersdic, resheadersdic, entheadersdic,
              genheadersdic, num, pathdic, cookie_dict, address):
    f_flag = 0
    flag = 0
    try:
        filename, flag = pathreturn(message[1], flag)
        filename1 = str(pathdic['docpath']) + '/' + filename
        #print filename1
        f = open(filename1)
    except:
        loc, code = compressedclean.checkredirect(filename, pathdic)
        if loc != 'N/A' and code != 000:
            resheadersdic['Location'] = loc
            rv = codes.error_3(code, 'confiles/error/' + str(code) + '.html',
                               reqheadersdic, resheadersdic, entheadersdic,
                               genheadersdic, num)
            return rv, str(code)
        #print filename1
        rv = codes.error_4(404, "confiles/error/404.html", reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '404'
    if len(message) > 3:
        codes.parseHeaders("", headers[1:-2], reqheadersdic, '')
        #print reqheadersdic
    #print flag
    if ((reqheadersdic['Host'] == ''
         or reqheadersdic['Host'] != 'localhost:1234') and flag == 0):
        rv = codes.error_4(400, 'confiles/error/400.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '400'
    un, pw, ty = compressedclean.checkprotect(filename, pathdic)
    if un != 'N/A' and pw != 'N/A' and reqheadersdic[
            'Authorization'] == '' and ty == 'protect':
        resheadersdic[
            'WWW-Authenticate'] = 'Basic realm="Access to the protected file", charset="UTF-8"'
        rv = codes.error_4(401, 'confiles/error/401.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '401'
    if un != 'N/A' and pw != 'N/A' and reqheadersdic[
            'Authorization'] != '' and ty == 'protect':
        un1, pw1 = codes.parseHeaders("", "", reqheadersdic, 'auth')
        #print un,pw
        if pw1 != pw:
            rv = codes.error_4(403, 'confiles/error/403.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '403'
    if un != 'N/A' and pw != 'N/A' and reqheadersdic[
            'Proxy-Authorization'] == '' and ty == 'proxy':
        resheadersdic[
            'Proxy-Authenticate'] = 'Basic realm="Access to the protected file", charset="UTF-8"'
        rv = codes.error_4(407, 'confiles/error/407.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '407'
    if un != 'N/A' and pw != 'N/A' and reqheadersdic[
            'Proxy-Authorization'] != '' and ty == 'proxy':
        un1, pw1 = codes.parseHeaders("", "", reqheadersdic, 'pauth')
        #print un,pw
        if pw1 != pw:
            rv = codes.error_4(403, 'confiles/error/403.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '403'
    #print(reqheadersdic)
    enco = priority = lang = chars = {}
    ranges = []

    if (reqheadersdic['Accept-Encoding'] != ''):
        enco = codes.parseHeaders("", "", reqheadersdic, 'enco')
    if (reqheadersdic['Accept'] != ""):
        priority = codes.parseHeaders("", "", reqheadersdic, 'split')
    if (reqheadersdic['Accept-Language'] != ""):
        lang = codes.parseHeaders("", "", reqheadersdic, 'lang')
    if (reqheadersdic['Accept-Charset'] != ""):
        chars = codes.parseHeaders("", "", reqheadersdic, 'ch')
    if (reqheadersdic['Range'] != "" and reqheadersdic['If-Range'] == ""):
        ranges = codes.parseHeaders(filename1, "", reqheadersdic, "range")
    check = checkcontentnego(filename, pathdic)
    if check == 'A':
        filen = compressedclean.contentnego(filename, reqheadersdic,
                                            resheadersdic, entheadersdic, enco,
                                            priority, lang, chars)
        if filen == 'N/A':
            rv = codes.error_4(406, 'confiles/error/406.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '406'
    if check == 'N/A':
        filen = filename
    #print filen
    if filen != filename:
        entheadersdic['Content-Location'] = filen
    filen1 = pathdic['docpath'] + '/' + filen
    if reqheadersdic['If-Modified-Since'] != '' and reqheadersdic[
            'If-Unmodified-Since'] == '' and reqheadersdic['If-Range'] == '':
        dt = os.path.getmtime(filen1)
        date1 = str(datetime.datetime.utcfromtimestamp(dt))
        given_date = str(
            datetime.datetime.strptime(reqheadersdic['If-Modified-Since'],
                                       '%a, %d %b %Y %H:%M:%S GMT'))
        if date1 > given_date:
            rv = codes.success_200(200, filen1, reqheadersdic, resheadersdic,
                                   entheadersdic, genheadersdic, ranges, num,
                                   cookie_dict, address)
            code = '200'
        else:
            rv = codes.error_3(304, '', reqheadersdic, resheadersdic,
                               entheadersdic, genheadersdic, num)
            return rv, '304'
        f_flag = 1
    if reqheadersdic['If-Unmodified-Since'] != '' and reqheadersdic[
            'If-Modified-Since'] == '' and reqheadersdic['If-Range'] == '':
        dt = os.path.getmtime(filen1)
        #print filen1
        date1 = str(datetime.datetime.utcfromtimestamp(dt))
        given_date = str(
            datetime.datetime.strptime(reqheadersdic['If-Unmodified-Since'],
                                       '%a, %d %b %Y %H:%M:%S GMT'))
        #print date1
        if date1 < given_date:
            rv = codes.success_200(200, filen1, reqheadersdic, resheadersdic,
                                   entheadersdic, genheadersdic, '', num,
                                   cookie_dict, address)
            code = '200'
        else:
            rv = codes.error_4(412, 'confiles/error/412.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '412'
        f_flag = 1
    if (reqheadersdic['Range'] != ""):
        ranges = codes.parseHeaders(filen1, "", reqheadersdic, "range")
        if ranges == []:
            rv = codes.error_4(416, 'confiles/error/416.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '416'
        else:
            if reqheadersdic['If-Range'] == '':
                entity = codes.read_data(ranges, filen1, entheadersdic,
                                         genheadersdic)
                rv = codes.success_200(206, entity, reqheadersdic,
                                       resheadersdic, entheadersdic,
                                       genheadersdic, ranges, num, cookie_dict,
                                       address)
                code = '206'
            elif reqheadersdic['If-Range'] != '' and reqheadersdic[
                    'If-Unmodified-Since'] == '' and reqheadersdic[
                        'If-Modified-Since'] == '':
                dt = os.path.getmtime(filen1)
                date1 = str(datetime.datetime.utcfromtimestamp(dt))
                given_date = str(
                    datetime.datetime.strptime(reqheadersdic['If-Range'],
                                               '%a, %d %b %Y %H:%M:%S GMT'))
                if date1 < given_date:
                    entity = codes.read_data(ranges, filen1, entheadersdic,
                                             genheadersdic)
                    rv = codes.success_200(206, entity, reqheadersdic,
                                           resheadersdic, entheadersdic,
                                           genheadersdic, ranges, num,
                                           cookie_dict, address)
                    code = '206'
                else:
                    rv = codes.success_200(200, filen1, reqheadersdic,
                                           resheadersdic, entheadersdic,
                                           genheadersdic, '', num, cookie_dict,
                                           address)
                    code = '200'
        f_flag = 1
    if (f_flag == 0):
        rv = codes.success_200(200, filen1, reqheadersdic, resheadersdic,
                               entheadersdic, genheadersdic, '', num,
                               cookie_dict, address)
        code = 200
    return rv, code