예제 #1
0
def handle_request(req, filename = None):
    if req == '':
        log.alert('Empty body received. Filename: %s' % filename)
        return False

    keys = gpg.get_keys(req)
    (em, body) = gpg.verify_sig(req)
    if not em:
        log.alert("Invalid signature, missing/untrusted key. Keys in gpg batch: '%s'" % keys)
        return False
    user = acl.user_by_email(em)
    if user == None:
        # FIXME: security email here
        log.alert("'%s' not in acl. Keys in gpg batch: '%s'" % (em, keys))
        return False

    acl.set_current_user(user)
    status.push("request from %s" % user.login)
    r = request.parse_request(body)
    if r.kind == 'group':
        handle_group(r, user)
    elif r.kind == 'notification':
        handle_notification(r, user)
    else:
        msg = "%s: don't know how to handle requests of this kind '%s'" \
                        % (user.get_login(), r.kind)
        log.alert(msg)
        m = user.message_to()
        m.set_headers(subject = "unknown request")
        m.write_line(msg)
        m.send()
    status.pop()
    return True
예제 #2
0
def handle_request(req, filename=None):
    if req == '':
        log.alert('Empty body received. Filename: %s' % filename)
        return False

    keys = gpg.get_keys(req)
    (em, body) = gpg.verify_sig(req)
    if not em:
        log.alert(
            "Invalid signature, missing/untrusted key. Keys in gpg batch: '%s'"
            % keys)
        return False
    user = acl.user_by_email(em)
    if user == None:
        # FIXME: security email here
        log.alert("'%s' not in acl. Keys in gpg batch: '%s'" % (em, keys))
        return False

    acl.set_current_user(user)
    status.push("request from %s" % user.login)
    r = request.parse_request(body)
    if r.kind == 'group':
        handle_group(r, user)
    elif r.kind == 'notification':
        handle_notification(r, user)
    else:
        msg = "%s: don't know how to handle requests of this kind '%s'" \
                        % (user.get_login(), r.kind)
        log.alert(msg)
        m = user.message_to()
        m.set_headers(subject="unknown request")
        m.write_line(msg)
        m.send()
    status.pop()
    return True
예제 #3
0
파일: client.py 프로젝트: wilfreddv/mercury
    def handle_request(self):
        BASE_DIR = config['HOME_DIR']
        # If it's static, we can just route it, otherwise load that onto the framework

        request = parse_request(self.request)

        if 'Accept' in request:
            mime = request['Accept'].split(',')[0]
        else:
            mime = "*/*"

        if '400' in request:
            self.send(Response(400, "Bad Request"))
            status_code = 400
            return

        l = Loader(request)
        response = Response(l.status, l.data, content_type=mime)
        status_code = l.status

        self._log_data += f" {status_code}"
        self.send(response)
  def wait_connections(self):
     # Main loop awaiting connections
     self.socket.listen(1) # maximum number of queued connections
     refresh30 = '<meta http-equiv="refresh" content="300">\n'
     error404 = '404 - Error'

     counting = 0
     while True:
         counting += 1
         print ("Wait ", counting)
         try:
            self.conn, self.addr = self.socket.accept()
         except KeyboardInterrupt:
            return
         except: # Timeout
            continue

         req = self.conn.readline()
         # conn - socket to client // addr - clients address
         while True:
             h = self.conn.readline()
             if not h or h == b'\r\n':
                 break

         # determine request method (GET / POST are supported)
         r = parse_request(req)
         if r == None:
             header = httpheader(404, self.title)
             content = error404
             self.http_send(header, content, self.footer)
         elif r['uri'] == b'/index' :
             header = httpheader(200, self.title, refresh=refresh30)
             self.http_send(header, cb_index(), self.footer)
         elif r['uri'] == b'/temperature' or r['uri'] == b'/' :
             content = cb_temperature()
             header = httpheader(200, self.title, refresh=refresh30)
             self.http_send(header, content, self.footer)
         elif r['uri'] == b'/j' :
             content = cb_temperature_json()
             header = httpheader(200, self.title, extension='j')
             self.http_send(header, content, [])
         elif r['uri'] == b'/help':
             content = cb_help()
             header = httpheader(200, self.title)
             self.http_send(header, content, self.footer)
         elif r['uri'] == b'/status':
             header = httpheader(200, self.title)
             self.http_send(header, cb_status(), self.footer)
         elif b'/conf' in r['uri']:
             if 'key' in r['args'] and 'value' in r['args']:
               header = httpheader(302, self.title)
               content = cb_setparam(r['args']['key'], r['args']['value'])
             elif 'key' in r['args'] :
               header = httpheader(200, self.title)
               content = cb_setparam(r['args']['key'], None)
             else:
               header = httpheader(200, self.title)
               content = cb_setparam(None, None)
             self.title = config.get_config('place') # just in case
             self.http_send(header, content, self.footer)

         elif r['uri'] == b'/ssid' :
             header = httpheader(200, self.title)
             content = cb_listssid()
             self.http_send(header, content, self.footer)

         elif r['uri'] == b'/setconf' :
             header = httpheader(200, self.title)
             content = cb_setconf()
             self.http_send(header, content, self.footer)
         elif r['uri'] == b'/reboot' :
             header = httpheader(200, self.title)
             content = '<h2><a href="/">Reboot in 2\"</a></h2></div>'
             self.http_send(header, content, self.footer)
             time.sleep(2)
             import machine
             machine.reset()
         elif r['file'] != b'':
             myfile = r['file']
             try:
                if myfile == b'port_config.py': raise Exception
                with open(myfile, 'r') as f:
                    content = f.readlines()
                header = httpheader(200, self.title)
             except:
                header = httpheader(404, self.title)
                content = 'No such file %s' % myfile
             self.http_send(header, content, self.footer)
         else:
             header = httpheader(404, self.title)
             content = error404
             self.http_send(header, content, self.footer)

         # At end of loop just close socket and collect garbage
         self.conn.close()
         gc.collect()
예제 #5
0
    def wait_connections(self, interface):

        # Main loop awaiting connections
        refresh30 = '<meta http-equiv="refresh" content="300">\r\n'
        redirect2 = '<meta http-equiv="refresh" content="2; url=/"/>\r\n'
        error404 = '404 - Error'

        while True:
            if not interface.isconnected():
                raise OSError('Lost connection')

            print('Wait')

            try:
                self.conn, self.addr = self.socket.accept()
                req = self.conn.readline()
            except KeyboardInterrupt:
                raise OSError('Interrupt')
            except:
                return

            l = 0
            while True:
                h = self.conn.readline()
                if not h or h == b'\r\n':
                    break
                if 'Content-Length: ' in h:
                    try:
                        l = int(h[16:-2])
                    except:
                        continue
            if l:
                fullreq = self.conn.read(l)
                print(fullreq)

            # Some defaults that can be changed in progress
            code = 200
            extension = 'h'
            refresh = ''

            # determine request method (GET / POST are supported)
            r = parse_request(req)
            if r == None:
                code = 404
                content = error404
            elif r['method'] == 'POST' or r['method'] == 'post':
                print(h)
                content = cb_setconf(None, None)
            elif r['uri'] == b'/temperature' or r['uri'] == b'/':
                refresh = refresh30
                content = cb_temperature()
            elif r['uri'] == b'/j':
                extension = 'j'
                content = cb_temperature_json()
            elif r['uri'] == b'/getconf':
                content = cb_getconf()
            elif b'/setconf' in r['uri']:
                if 'key' in r['args'] and 'value' in r['args']:
                    content = cb_setconf(r['args']['key'], r['args']['value'])
                elif 'key' in r['args']:
                    content = cb_setconf(r['args']['key'], None)
                else:
                    content = cb_setconf(None, None)
            elif r['uri'] == b'/status':
                content = cb_status()
            elif r['uri'] == b'/webrepl':
                import webrepl
                webrepl.start()
                refresh = redirect2
                code = 302
                content = '<h2>Webrepl +5min</h2></div>'
            elif r['uri'] == b'/5min':
                self.socket.settimeout(60)
                refresh = redirect2
                code = 302
                content = '<h2>+5min</h2></div>'
            elif r['uri'] == b'/reboot':
                content = '<h2>Reboot</h2></div>'
                self.http_send(code, content, extension, redirect2)
                self.conn.close()
                machine.reset()
            elif r['file'] != b'':
                myfile = r['file']
                code = 200
                content = cb_open(myfile)
            else:
                code = 404
                content = error404

            # At end of loop just close socket and collect garbage
            self.http_send(code, content, extension, refresh)
            self.conn.close()
            gc.collect()
    def wait_connections(self, interface, sleeptime):

        # Main loop awaiting connections
        refresh30 = '<meta http-equiv="refresh" content="300">\r\n'
        redirect2 = '<meta http-equiv="refresh" content="2; url=/"/>\r\n'
        error404 = '404 - Error'

        while True:

            try:
                self.conn, self.addr = self.socket.accept()
            except KeyboardInterrupt:
                return
            except:
                continue

            try:
                req = self.conn.readline()
            except:
                continue

            # conn - socket to client // addr - clients address
            while True:
                h = self.conn.readline()
                if not h or h == b'\r\n':
                    break

            # Some defaults that can be changed in progress
            code = 200
            extension = 'h'
            refresh = ''

            # determine request method (GET / POST are supported)
            r = parse_request(req)
            if r == None:
                code = 404
                content = error404
            elif r['uri'] == b'/' or r['uri'] == b'/index':
                content = '<h2>%s %s</h2>' % (self.title, control.status())
            elif r['uri'] == b'/on':
                content = '<h2>Switch %s</h2>' % control.switch_on()
            elif r['uri'] == b'/off':
                content = '<h2>Switch %s</h2>' % control.switch_off()
            elif r['uri'] == b'/temperature':
                content = cb_temperature()
                content = ''
            elif r['uri'] == b'/j':
                extension = 'j'
                content = cb_temperature_json()
                content = ''
            elif r['uri'] == b'/getconf':
                content = cb_getconf()
            elif b'/setconf' in r['uri']:
                if 'key' in r['args'] and 'value' in r['args']:
                    content = cb_setconf(r['args']['key'], r['args']['value'])
                elif 'key' in r['args']:
                    content = cb_setconf(r['args']['key'], None)
                else:
                    content = cb_setconf(None, None)
                self.title = config.get_config('place')  # just in case
            elif r['uri'] == b'/status':
                content = cb_status()
            elif r['uri'] == b'/webrepl':
                import webrepl
                webrepl.start()
                sleeptime = 299  # almost 5 minutes
                refresh = redirect2
                code = 302
                content = '<h2>Webrepl +5min</h2></div>'
            elif r['uri'] == b'/5min':
                sleeptime = 299  # almost 5 minutes
                refresh = redirect2
                code = 302
                content = '<h2>+5min</h2></div>'
            elif r['uri'] == b'/reboot':
                content = '<h2>Reboot</h2></div>'
                self.http_send(code, content, extension, redirect2)
                self.conn.close()
                machine.reset()
            elif r['file'] != b'':
                myfile = r['file']
                code = 200
                content = cb_open(myfile)
            else:
                code = 404
                content = error404

            # At end of loop just close socket and collect garbage
            self.http_send(code, content, extension, refresh)
            self.conn.close()
            gc.collect()