Exemplo n.º 1
0
 def do_POST(self):
     maxlen = 5000000
     (path, sep, query) = self.path.partition('?')
     if not path.startswith(self.server.navurl):
         self.send_error(404, "unsupported post url")
         return
     try:
         ctype, pdict = cgi.parse_header(
             self.headers.getheader('content-type'))
         if ctype == 'multipart/form-data':
             postvars = cgi.parse_multipart(self.rfile, pdict)
         elif ctype == 'application/x-www-form-urlencoded':
             length = int(self.headers.getheader('content-length'))
             if length > maxlen:
                 raise Exception("too much data" + unicode(length))
             postvars = cgi.parse_qs(self.rfile.read(length),
                                     keep_blank_values=1)
         elif ctype == 'application/json':
             length = int(self.headers.getheader('content-length'))
             if length > maxlen:
                 raise Exception("too much data" + unicode(length))
             postvars = {'_json': self.rfile.read(length)}
         else:
             postvars = {}
         requestParam = urlparse.parse_qs(query, True)
         requestParam.update(postvars)
         self.handleNavRequest(path, requestParam)
     except Exception as e:
         txt = traceback.format_exc()
         AVNLog.ld("unable to process request for ", path, query, txt)
         self.send_response(500, txt)
         self.end_headers()
         return
Exemplo n.º 2
0
    def handleNavRequest(self, path, requestParam):
        #check if we have something behind the navurl
        #treat this as a filename and set it ion the request parameter
        fname = path[(len(self.server.navurl) + 1):]
        if fname is not None and fname != "":
            fname = fname.split('?', 1)[0]
            if fname != "":
                if requestParam.get('filename') is None:
                    requestParam['filename'] = [fname.encode('utf-8')]
        requestType = requestParam.get('request')
        if requestType is None:
            requestType = 'gps'
        else:
            requestType = requestType[0]
        AVNLog.ld('navrequest', requestParam)
        try:
            rtj = None
            if requestType == 'gps' or requestType == 'self':
                rtj = self.handleGpsRequest(requestParam)
            elif requestType == 'nmeaStatus':
                rtj = self.handleNmeaStatus(requestParam)
            elif requestType == 'ais':
                rtj = self.handleAISRequest(requestParam)
            elif requestType == 'status':
                rtj = self.handleStatusRequest(requestParam)
            elif requestType == 'debuglevel' or requestType == 'loglevel':
                rtj = self.handleDebugLevelRequest(requestParam)
            elif requestType == 'listdir' or requestType == 'list':
                rtj = self.handleListDir(requestParam)
            elif requestType == 'download':
                #download requests are special
                # the dow not return json...
                self.handleDownloadRequest(requestParam)
                return
            elif requestType == 'upload':
                try:
                    rtj = self.handleUploadRequest(requestParam)
                except Exception as e:
                    AVNLog.error("upload error: %s", unicode(e))
                    rtj = json.dumps({'status': unicode(e)})
            elif requestType == 'delete':
                rtj = self.handleDeleteRequest(requestParam)

            elif requestType == 'capabilities':
                rtj = self.handleCapabilityRequest(requestParam)
            elif requestType == 'api':
                #new handling for dedicated requests for some handler
                type = self.getRequestParam(requestParam, 'type')
                rtj = self.handleSpecificRequest(requestParam, type)
            else:
                #legacy: have the api type as requestType
                rtj = self.handleSpecificRequest(requestParam, requestType)
            self.sendNavResponse(rtj, requestParam)
        except Exception as e:
            text = unicode(e)
            rtj = json.dumps(
                AVNUtil.getReturnData(error=text,
                                      stack=traceback.format_exc()))
            self.sendNavResponse(rtj, requestParam)
            return
Exemplo n.º 3
0
 def __init__(self, request, client_address, server):
     #allow write buffering
     #see https://lautaportti.wordpress.com/2011/04/01/basehttprequesthandler-wastes-tcp-packets/
     self.wbufsize = -1
     self.id = None
     self.getRequestParam = AVNUtil.getHttpRequestParam
     AVNLog.ld("receiver thread started", client_address)
     SimpleHTTPServer.SimpleHTTPRequestHandler.__init__(
         self, request, client_address, server)
Exemplo n.º 4
0
 def sendNavResponse(self, rtj, requestParam):
     if not rtj is None:
         self.send_response(200)
         if not requestParam.get('callback') is None:
             rtj = "%s(%s);" % (requestParam.get('callback'), rtj)
             self.send_header("Content-type", "text/javascript")
         else:
             self.send_header("Content-type", "application/json")
         self.send_header("Content-Length", str(len(rtj)))
         self.send_header("Last-Modified", self.date_time_string())
         self.end_headers()
         self.wfile.write(rtj)
         AVNLog.ld("nav response", rtj)
     else:
         raise Exception("empty response")