Exemplo n.º 1
0
 def _processRequest(self):
     try :
         response = MicroWebSrv._response(self)
         if self._parseFirstLine(response) :
             if self._parseHeader(response) :
                 upg = self._getConnUpgrade()
                 if not upg :
                     routeHandler, routeArgs = self._microWebSrv.GetRouteHandler(self._resPath, self._method)
                     if routeHandler :
                         if routeArgs is not None:
                             routeHandler(self, response, routeArgs)
                         else:
                             routeHandler(self, response)
                     elif self._method.upper() == "GET" :
                         filepath = self._microWebSrv._physPathFromURLPath(self._resPath)
                         if filepath :
                             if MicroWebSrv._isPyHTMLFile(filepath) :
                                 response.WriteResponsePyHTMLFile(filepath)
                             else :
                                 contentType = self._microWebSrv.GetMimeTypeFromFilename(filepath)
                                 if contentType :
                                     if self._microWebSrv.LetCacheStaticContentLevel > 0 :
                                         if self._microWebSrv.LetCacheStaticContentLevel > 1 and \
                                            'if-modified-since' in self._headers :
                                             response.WriteResponseNotModified()
                                         else:
                                             headers = { 'Last-Modified' : 'Fri, 1 Jan 2018 23:42:00 GMT', \
                                                         'Cache-Control' : 'max-age=315360000' }
                                             response.WriteResponseFile(filepath, contentType, headers)
                                     else :
                                         response.WriteResponseFile(filepath, contentType)
                                 else :
                                     response.WriteResponseForbidden()
                         else :
                             response.WriteResponseNotFound()
                     else :
                         response.WriteResponseMethodNotAllowed()
                 elif upg == 'websocket' and 'MicroWebSocket' in globals() \
                      and self._microWebSrv.AcceptWebSocketCallback :
                         MicroWebSocket( socket         = self._socket,
                                         httpClient     = self,
                                         httpResponse   = response,
                                         maxRecvLen     = self._microWebSrv.MaxWebSocketRecvLen,
                                         threaded       = self._microWebSrv.WebSocketThreaded,
                                         acceptCallback = self._microWebSrv.AcceptWebSocketCallback )
                         return
                 else :
                     response.WriteResponseNotImplemented()
             else :
                 response.WriteResponseBadRequest()
     except :
         response.WriteResponseInternalServerError()
     try :
         if self._socketfile is not self._socket:
             self._socketfile.close()
         self._socket.close()
     except :
         pass
Exemplo n.º 2
0
 def _processRequest(self):
     try:
         response = MicroWebSrv._response(self)
         if self._parseFirstLine(response):
             if self._parseHeader(response):
                 upg = self._getConnUpgrade()
                 if not upg:
                     routeHandler, routeArgs = self._microWebSrv.GetRouteHandler(
                         self._resPath, self._method)
                     if routeHandler:
                         if routeArgs is not None:
                             routeHandler(self, response, routeArgs)
                         else:
                             routeHandler(self, response)
                     elif self._method.upper() == "GET":
                         filepath = self._microWebSrv._physPathFromURLPath(
                             self._resPath)
                         if filepath:
                             if MicroWebSrv._isPyHTMLFile(filepath):
                                 response.WriteResponsePyHTMLFile(
                                     filepath)
                             else:
                                 contentType = self._microWebSrv.GetMimeTypeFromFilename(
                                     filepath)
                                 if contentType:
                                     response.WriteResponseFile(
                                         filepath, contentType)
                                 else:
                                     response.WriteResponseForbidden()
                         else:
                             response.WriteResponseNotFound()
                     else:
                         response.WriteResponseMethodNotAllowed()
                 elif upg == 'websocket' and 'MicroWebSocket' in globals() \
                      and self._microWebSrv.AcceptWebSocketCallback :
                     MicroWebSocket(
                         socket=self._socket,
                         httpClient=self,
                         httpResponse=response,
                         maxRecvLen=self._microWebSrv.
                         MaxWebSocketRecvLen,
                         threaded=self._microWebSrv.WebSocketThreaded,
                         acceptCallback=self._microWebSrv.
                         AcceptWebSocketCallback)
                     return
                 else:
                     response.WriteResponseNotImplemented()
             else:
                 response.WriteResponseBadRequest()
     except:
         response.WriteResponseInternalServerError()
     try:
         if self._socketfile is not self._socket:
             self._socketfile.close()
         self._socket.close()
     except:
         pass
Exemplo n.º 3
0
 def _processRequest(self):
     try:
         response = MicroWebSrv._response(self)
         if self._parseFirstLine(response):
             if self._parseHeader(response):
                 upg = self._getConnUpgrade()
                 if upg is None:
                     routeHandler = self._microWebSrv.GetRouteHandler(
                         self._resPath, self._method)
                     if routeHandler is not None:
                         routeHandler(self, response)
                     elif self._method.upper() == "GET":
                         filepath = self._microWebSrv._physPathFromURLPath(
                             self._resPath)
                         if filepath is not None:
                             if self._microWebSrv._isPyHTMLFile(
                                     filepath):
                                 response.WriteResponsePyHTMLFile(
                                     filepath)
                             else:
                                 contentType = self._microWebSrv.GetMimeTypeFromFilename(
                                     filepath)
                                 if contentType is not None:
                                     response.WriteResponseFile(
                                         filepath, contentType)
                                 else:
                                     response.WriteResponseForbidden()
                         else:
                             response.WriteResponseNotFound()
                     else:
                         response.WriteResponseMethodNotAllowed()
                 elif upg == 'websocket' and 'MicroWebSocket' in globals(
                 ):
                     MicroWebSocket(self._socket, self, response)
                     return
                 else:
                     response.WriteResponseNotImplemented()
             else:
                 response.WriteResponseBadRequest()
     except:
         response.WriteResponseInternalServerError()
     try:
         self._socket.close()
     except:
         pass
Exemplo n.º 4
0
 def _processRequest(self):
     try:
         response = MicroWebSrv._response(self)
         if self._parseFirstLine(response):
             if self._parseHeader(response):
                 upg = self._getConnUpgrade()
                 if not upg:
                     routeHandler, routeArgs = self._microWebSrv.GetRouteHandler(
                         self._resPath, self._method)
                     if routeHandler:
                         try:
                             if routeArgs is not None:
                                 routeHandler(self, response, routeArgs)
                             else:
                                 routeHandler(self, response)
                         except Exception as ex:
                             print(
                                 "MicroWebSrv handler exception:\r\n  - In route %s %s\r\n  - %s"
                                 % (self._method, self._resPath, ex))
                             raise ex
                     elif self._method.upper() == "GET":
                         filepath = self._microWebSrv._physPathFromURLPath(
                             self._resPath)
                         if filepath:
                             if MicroWebSrv._isPyHTMLFile(filepath):
                                 response.WriteResponsePyHTMLFile(
                                     filepath)
                             else:
                                 contentType = (
                                     self._microWebSrv.
                                     GetMimeTypeFromFilename(filepath))
                                 if contentType:
                                     if (self._microWebSrv.
                                             LetCacheStaticContentLevel
                                             > 0):
                                         if (self._microWebSrv.
                                                 LetCacheStaticContentLevel
                                                 > 1
                                                 and "if-modified-since"
                                                 in self._headers):
                                             response.WriteResponseNotModified(
                                             )
                                         else:
                                             headers = {
                                                 "Last-Modified":
                                                 "Fri, 1 Jan 2018 23:42:00 GMT",
                                                 "Cache-Control":
                                                 "max-age=315360000",
                                             }
                                             response.WriteResponseFile(
                                                 filepath, contentType,
                                                 headers)
                                     else:
                                         response.WriteResponseFile(
                                             filepath, contentType)
                                 else:
                                     response.WriteResponseForbidden()
                         else:
                             response.WriteResponseNotFound()
                     else:
                         response.WriteResponseMethodNotAllowed()
                 elif (upg == "websocket"
                       and "MicroWebSocket" in globals()
                       and self._microWebSrv.AcceptWebSocketCallback):
                     MicroWebSocket(
                         socket=self._socket,
                         httpClient=self,
                         httpResponse=response,
                         maxRecvLen=self._microWebSrv.
                         MaxWebSocketRecvLen,
                         threaded=self._microWebSrv.WebSocketThreaded,
                         acceptCallback=self._microWebSrv.
                         AcceptWebSocketCallback,
                     )
                     return
                 else:
                     response.WriteResponseNotImplemented()
             else:
                 response.WriteResponseBadRequest()
     except:
         response.WriteResponseInternalServerError()
     try:
         if self._socketfile is not self._socket:
             self._socketfile.close()
         self._socket.close()
     except:
         pass
Exemplo n.º 5
0
        def _processRequest(self):
            try:
                response = MicroWebSrv._response(self)
                if self._parseFirstLine(response):
                    if self._parseHeader(response):
                        upg = self._getConnUpgrade()
                        if not upg:
                            routeHandler, routeArgs = self._microWebSrv.GetRouteHandler(
                                self._resPath, self._method)
                            if routeHandler:
                                if routeArgs is not None:
                                    routeHandler(self, response, routeArgs)
                                else:
                                    routeHandler(self, response)
                            elif self._method.upper() == "GET":
                                encodings = [
                                    enc.strip().split(";")[0]
                                    for enc in self._headers.get(
                                        "accept-encoding", "").split(",")
                                ]
                                filepath = self._microWebSrv._physPathFromURLPath(
                                    self._resPath, encodings)
                                if filepath:
                                    if MicroWebSrv._isPyHTMLFile(filepath):
                                        response.WriteResponsePyHTMLFile(
                                            filepath)
                                    else:
                                        encoding = self._microWebSrv.GetEncodingFromFilename(
                                            filepath)
                                        contentType = self._microWebSrv.GetMimeTypeFromFilename(
                                            filepath, encoding)
                                        if contentType:
                                            headers = {}
                                            if encoding:
                                                headers[
                                                    "Content-Encoding"] = encoding

                                            cache_level = self._microWebSrv.LetCacheStaticContentLevel

                                            for path, value in self._microWebSrv.StaticCacheByPath:
                                                if filepath.startswith(path):
                                                    cache_level = value
                                                    break

                                            if cache_level > 0:
                                                if cache_level > 1 and \
                                                   'if-modified-since' in self._headers :
                                                    response.WriteResponseNotModified(
                                                    )
                                                else:
                                                    headers[
                                                        'Last-Modified'] = 'Fri, 1 Jan 2018 23:42:00 GMT'
                                                    headers[
                                                        'Cache-Control'] = 'max-age=315360000'
                                            response.WriteResponseFile(
                                                filepath, contentType, headers)
                                        else:
                                            response.WriteResponseForbidden()
                                else:
                                    response.WriteResponseNotFound()
                            else:
                                response.WriteResponseMethodNotAllowed()
                        elif upg == 'websocket' and 'MicroWebSocket' in globals() \
                             and self._microWebSrv.AcceptWebSocketCallback :
                            MicroWebSocket(
                                socket=self._socket,
                                httpClient=self,
                                httpResponse=response,
                                maxRecvLen=self._microWebSrv.
                                MaxWebSocketRecvLen,
                                threaded=self._microWebSrv.WebSocketThreaded,
                                acceptCallback=self._microWebSrv.
                                AcceptWebSocketCallback)
                            return
                        else:
                            response.WriteResponseNotImplemented()
                    else:
                        response.WriteResponseBadRequest()
            except Exception as e:
                sys.print_exception(e)
                response.WriteResponseInternalServerError()
            try:
                if self._socketfile is not self._socket:
                    self._socketfile.close()
                self._socket.close()
            except:
                pass
Exemplo n.º 6
0
        def _processRequest(self):
            if self._debug:
                print("  [{}] [CLIENT] Processing request".format(
                    time.ticks_ms()))
            try:
                response = MicroWebSrv._response(self)
                if self._parseFirstLine(response):
                    if self._parseHeader(response):
                        if self._debug:
                            print(
                                "  [{}] [CLIENT] Method: [{}], Headers: [{}]".
                                format(time.ticks_ms(), self._method.upper(),
                                       self._headers))
                        upg = self._getConnUpgrade()
                        if not upg:
                            routeHandler, routeArgs = self._microWebSrv.GetRouteHandler(
                                self._resPath, self._method)
                            if routeHandler:
                                if routeArgs is not None:
                                    routeHandler(self, response, routeArgs)
                                else:
                                    routeHandler(self, response)
                            elif self._method.upper() == "GET":
                                filepath = self._microWebSrv._physPathFromURLPath(
                                    self._resPath)
                                if filepath:
                                    if self._debug:
                                        print(
                                            "  [{}] [CLIENT] GET: '{}'".format(
                                                time.ticks_ms(), filepath))
                                    if MicroWebSrv._isPyHTMLFile(filepath):
                                        response.WriteResponsePyHTMLFile(
                                            filepath)
                                    else:
                                        contentType = self._microWebSrv.GetMimeTypeFromFilename(
                                            filepath)
                                        if contentType:
                                            response.WriteResponseFile(
                                                filepath, contentType)
                                        else:
                                            response.WriteResponseForbidden()
                                else:
                                    response.WriteResponseNotFound()
                            else:
                                response.WriteResponseMethodNotAllowed()
                        elif upg == 'websocket' and 'MicroWebSocket' in globals(
                        ) and self._microWebSrv.AcceptWebSocketCallback:
                            MicroWebSocket(
                                socket=self._socket,
                                httpClient=self,
                                httpResponse=response,
                                maxRecvLen=self._microWebSrv.
                                MaxWebSocketRecvLen,
                                threaded=self._microWebSrv.WebSocketThreaded,
                                acceptCallback=self._microWebSrv.
                                AcceptWebSocketCallback,
                                stackSize=self._microWebSrv.WebSocketStackSize,
                                debug=self._debug)
                            return
                        else:
                            if self._debug:
                                print(
                                    "  [{}] [CLIENT] Not implemented: upg={} [{}]"
                                    .format(time.ticks_ms(), upg, globals()))
                            response.WriteResponseNotImplemented()
                    else:
                        response.WriteResponseBadRequest()
            except:
                if self._debug:
                    print("  [{}] [CLIENT] Internal server error".format(
                        time.ticks_ms()))
                response.WriteResponseInternalServerError()

            time.sleep_ms(50)
            if self._debug:
                print("  [{}] [CLIENT] close socket {}".format(
                    time.ticks_ms(), self._socket.getFD()))
            n_try = 1
            while n_try < 5:
                try:
                    self._socket.close()
                    if self._debug:
                        print("  [{}] [CLIENT] Request processed ({})".format(
                            time.ticks_ms(), n_try))
                    return
                except:
                    n_try += 1
                    time.sleep_ms(100)
            if self._debug:
                print("  [{}] [CLIENT] Close socket error".format(
                    time.ticks_ms()))
Exemplo n.º 7
0
        def _processRequest(self) :
            try :
                response = MicroWebSrv._response(self)
                if self._parseFirstLine(response) :
                    if self._parseHeader(response) :
                        upg = self._getConnUpgrade()

                        if not upg :
                            routeHandler, routeArgs = self._microWebSrv.GetRouteHandler(self._resPath, self._method)
                            if routeHandler :
                                try :
                                    if routeArgs is not None:
                                        routeHandler(self, response, routeArgs)
                                    else :
                                        routeHandler(self, response)
                                except Exception as ex :
                                    print('MicroWebSrv handler exception:\r\n  - In route %s %s\r\n  - %s' % (self._method, self._resPath, ex))
                                    raise ex
                            elif self._method.upper() == "GET" :
                                compressed = 0
                                filepath   = self._microWebSrv._physPathFromURLPath(self._resPath)
                                filepathgz = self._microWebSrv._physPathFromURLPath("{0}.gz".format(self._resPath))
                                # Replace by GZ file only if does not exists uncompressed version
                                if filepathgz and not filepath:
                                    filepath = filepathgz
                                    compressed = 1
                                if filepath:
                                    if MicroWebSrv._isPyHTMLFile(filepath) :
                                        response.WriteResponsePyHTMLFile(filepath)
                                    else:
                                        contentType = self._microWebSrv.GetMimeTypeFromFilename(filepath if not compressed else filepath[:-3])
                                        if contentType:
                                            headers = dict()

                                            if compressed:
                                                headers['Content-Encoding'] = "gzip"

                                            if self._microWebSrv.LetCacheStaticContentLevel > 0 :
                                                if self._microWebSrv.LetCacheStaticContentLevel > 1 and \
                                                   'if-modified-since' in self._headers :
                                                    response.WriteResponseNotModified()
                                                else:
                                                    headers['Last-Modified'] = 'Fri, 1 Jan 2018 23:42:00 GMT'
                                                    headers['Cache-Control'] = 'max-age=315360000'
                                                    response.WriteResponseFile(filepath, contentType, headers)
                                            else :
                                                response.WriteResponseFile(filepath, contentType, headers)
                                        else :
                                            response.WriteResponseForbidden()
                                else :
                                    response.WriteResponseNotFound()
                            else :
                                response.WriteResponseMethodNotAllowed()
                        elif upg == 'websocket' and 'MicroWebSocket' in globals() \
                             and self._microWebSrv.AcceptWebSocketCallback :
                                MicroWebSocket( socket         = self._socket,
                                                httpClient     = self,
                                                httpResponse   = response,
                                                maxRecvLen     = self._microWebSrv.MaxWebSocketRecvLen,
                                                threaded       = self._microWebSrv.WebSocketThreaded,
                                                acceptCallback = self._microWebSrv.AcceptWebSocketCallback )
                                return
                        else :
                            response.WriteResponseNotImplemented()
                    else :
                        response.WriteResponseBadRequest()
            except :
                response.WriteResponseInternalServerError()
            try :
                if self._socketfile is not self._socket:
                    self._socketfile.close()
                self._socket.close()
            except :
                pass
        def _processRequest(self):
            """
            This private method of embedded class '_client' is responsible for processing HTTP requests from the client.

            :return:
            """
            try:
                response = MicroWebSrv._response(
                    self
                )  # create a response object template which is empty at this point.
                if self._parseFirstLine(response):
                    if self._parseHeader(response):
                        upg = self._getConnUpgrade(
                        )  # check to see if we can upgrade to web sockets.
                        if not upg:
                            routeHandler, routeArgs = self._microWebSrv.GetRouteHandler(
                                self._resPath, self._method)
                            if routeHandler:  # If we have a route handler function for this URL patth then use it to handle the response.
                                if routeArgs is not None:
                                    routeHandler(self, response, routeArgs)
                                else:
                                    routeHandler(self, response)
                            elif self._method.upper(
                            ) == "GET":  # We only allow default GET requests to the server if not handled explicitly
                                filepath = self._microWebSrv._physPathFromURLPath(
                                    self._resPath
                                )  # Get a file path if it is valid and exists
                                if filepath:
                                    if MicroWebSrv._isPyHTMLFile(filepath):
                                        response.WriteResponsePyHTMLFile(
                                            filepath)
                                    else:
                                        contentType = self._microWebSrv.GetMimeTypeFromFilename(
                                            filepath)
                                        if contentType:
                                            if self._microWebSrv.LetCacheStaticContentLevel > 0:
                                                if self._microWebSrv.LetCacheStaticContentLevel > 1 and \
                                                   'if-modified-since' in self._headers :
                                                    response.WriteResponseNotModified(
                                                    )
                                                else:
                                                    headers = { 'Last-Modified' : 'Fri, 1 Jan 2018 23:42:00 GMT', \
                                                                'Cache-Control' : 'max-age=315360000' }
                                                    response.WriteResponseFile(
                                                        filepath, contentType,
                                                        headers)
                                            else:
                                                response.WriteResponseFile(
                                                    filepath, contentType)
                                        else:
                                            response.WriteResponseForbidden()
                                else:
                                    response.WriteResponseNotFound()
                            else:
                                response.WriteResponseMethodNotAllowed()
                        elif upg == 'websocket' and 'MicroWebSocket' in globals() \
                             and self._microWebSrv.AcceptWebSocketCallback :
                            MicroWebSocket(
                                socket=self._socket,
                                httpClient=self,
                                httpResponse=response,
                                maxRecvLen=self._microWebSrv.
                                MaxWebSocketRecvLen,
                                threaded=self._microWebSrv.WebSocketThreaded,
                                acceptCallback=self._microWebSrv.
                                AcceptWebSocketCallback)
                            return
                        else:
                            response.WriteResponseNotImplemented()
                    else:
                        response.WriteResponseBadRequest()
            except:
                response.WriteResponseInternalServerError()
            try:
                if self._socketfile is not self._socket:
                    self._socketfile.close()
                self._socket.close()
            except:
                pass
Exemplo n.º 9
0
 def _processRequest(self) :
     try :
         response = MicroWebSrv._response(self)
         if self._parseFirstLine(response) : # retorna true ssi puede descomponer correctamente la solicitud del cliente.
             if self._parseHeader(response) : # retornara true ssi la solicitud del cliente contiene un get o un post
                 upg = self._getConnUpgrade()
                 #UPG PUEDE SER NONE O OTRA COSA.....
                 print('UPG:   ', upg)
                 if not upg :
                     # SOLAMENTE SI UPG ES NONE
                     routeHandler, routeArgs = self._microWebSrv.GetRouteHandler(self._resPath, self._method) # DEVUELVE LA RUTA DEL METODO SOLICITADO
                     print(routeHandler, routeArgs)
                     if routeHandler:# SI, SOLO SI ROUTEHANDLER NO ES NONE SI LO FUERA
                         if routeArgs is not None:
                             routeHandler(self, response, routeArgs)
                         else:
                             routeHandler(self, response)
                     elif self._method.upper() == "GET" : 
                         filepath = self._microWebSrv._physPathFromURLPath(self._resPath)
                         if filepath : # no none
                             if MicroWebSrv._isPyHTMLFile(filepath) :
                                 response.WriteResponsePyHTMLFile(filepath)
                             else :
                                 contentType = self._microWebSrv.GetMimeTypeFromFilename(filepath)
                                 if self._microWebSrv.LetCacheStaticContentLevel > 0 and contentType :
                                     if self._microWebSrv.LetCacheStaticContentLevel > 1 and 'if-modified-since' in self._headers :
                                         response.WriteResponseNotModified()
                                     else:
                                         header = {'Last-Modified':'Fri, 1 Jan 2018 23:42:00 GMT', \
                                                   'Cache-Control':'max-age=315360000'}
                                         response.WriteResponseFile(filepath, contentType, header)
                                 elif contentType :
                                     response.WriteResponseFile(filepath, contentType, header)
                                 else :
                                     response.WriteResponseForbidden()
                         else :
                             response.WriteResponseNotFound()
                     else :
                         #ERROR DE NO TENER LA RUTA
                         response.WriteResponseMethodNotAllowed()
                 elif upg == 'websocket' and 'MicroWebSocket' in globals() \
                      and self._microWebSrv.AcceptWebSocketCallback :## NO ESTA CONFIGURADO EN GLOBALS EL MICROWEBSOCKET POR TANTO 
                      ##NUNCA ENTRARA EN ESTA CONDICION! QUE SIGNIFICA QUE UPG==WEBSOCKET??? :X
                         print('se crea un nuevo socket solamente desde microwebsocket!')
                         MicroWebSocket( socket         = self._socket,
                                         httpClient     = self,
                                         httpResponse   = response,
                                         maxRecvLen     = self._microWebSrv.MaxWebSocketRecvLen,
                                         threaded       = self._microWebSrv.WebSocketThreaded,
                                         acceptCallback = self._microWebSrv.AcceptWebSocketCallback )
                         return
                 else :
                     response.WriteResponseNotImplemented()
             else :
                 response.WriteResponseBadRequest()
     except :
         response.WriteResponseInternalServerError()
     try :
         if self._socketfile is not self._socket:
             self._socketfile.close()
         self._socket.close()
     except :
         pass