<commit_before>#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4

import BaseHTTPServer, SocketServer
import select
import random
import re
import urllib

import rapidsms
from rapidsms.message import Message

msg_store = {}

class HttpHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def log_error (self, format, *args):
        self.server.backend.error(format, *args)

    def log_message (self, format, *args):
        self.server.backend.debug(format, *args)

    def do_GET(self):
        global msg_store
        # if the path is just "/" then start a new session
        # and redirect to that session's URL
        if self.path == "/":
            session_id = random.randint(100000, 999999)
            self.send_response(301)
            self.send_header("Location", "/%d/" % session_id)
            self.end_headers()
            return
        
        # if the path is of the form /integer/blah 
        # send a new message from integer with content blah
        send_regex = re.compile(r"^(/\w+/)/(\d+)/(.+)")
        match = send_regex.match(self.path)
        if match:
            # send the message
            backend = match.group(1)
            session_id = match.group(2)
            text = match.group(3)
            
            if text == "json_resp":
                self.send_response(200)
                self.send_header("Content-type", "text/html")
                self.end_headers()
                
                if msg_store.has_key(session_id) and len(msg_store[session_id]):
                        self.wfile.write("{'phone':'%s', 'message':'%s'}" % (session_id, str(msg_store[session_id].pop(0))))
                return
                
            # TODO watch out because urllib.unquote will blow up on unicode text 
            if backend:
                target = self.server.backend.router.get_backend(backend)
            else:
                target = self.server.backend
            msg = target.message(session_id, urllib.unquote(text))
            target.route(msg)
            # respond with the number and text 
            self.send_response(200)
            self.send_header("Content-type", "text/html")
            self.end_headers()
            self.wfile.write("{'phone':'%s', 'message':'%s'}" % (session_id, urllib.unquote(text)))
            return
            
        return
        
    def do_POST(self):
        # TODO move the actual sending over to here
        return<commit_msg>Make the changes to the http backend actually work.<commit_after>#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4

import BaseHTTPServer, SocketServer
import select
import random
import re
import urllib

import rapidsms
from rapidsms.message import Message

msg_store = {}

class HttpHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def log_error (self, format, *args):
        self.server.backend.error(format, *args)

    def log_message (self, format, *args):
        self.server.backend.debug(format, *args)

    def do_GET(self):
        global msg_store
        # if the path is just "/" then start a new session
        # and redirect to that session's URL
        if self.path == "/":
            session_id = random.randint(100000, 999999)
            self.send_response(301)
            self.send_header("Location", "/%d/" % session_id)
            self.end_headers()
            return
        
        # if the path is of the form /integer/blah 
        # send a new message from integer with content blah
        send_regex = re.compile(r"^(/[a-zA-Z]\w*/)?/(\d+)/(.+)")
        match = send_regex.match(self.path)
        if match:
            # send the message
            backend = match.group(1)
            session_id = match.group(2)
            text = match.group(3)
            
            if text == "json_resp":
                self.send_response(200)
                self.send_header("Content-type", "text/html")
                self.end_headers()
                
                if msg_store.has_key(session_id) and len(msg_store[session_id]):
                        self.wfile.write("{'phone':'%s', 'message':'%s'}" % (session_id, str(msg_store[session_id].pop(0))))
                return
                
            # TODO watch out because urllib.unquote will blow up on unicode text 
            if backend:
                target = self.server.backend.router.get_backend(backend)
            else:
                target = self.server.backend
            msg = target.message(session_id, urllib.unquote(text))
            target.route(msg)
            # respond with the number and text 
            self.send_response(200)
            self.send_header("Content-type", "text/html")
            self.end_headers()
            self.wfile.write("{'backend': '%s', 'phone':'%s', 'message':'%s'}" % (target.name, session_id, urllib.unquote(text)))
            return
            
        return
        
    def do_POST(self):
        # TODO move the actual sending over to here
        return
{'fraction_non_alphanumeric': 0.11066479212693311, 'fraction_numerical': 0.011247238401285399, 'mean_word_length': 1.86536248561565, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2108784', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1405, 'n_words': 350}