<commit_before>#!/usr/bin/env python
#-*-*- encoding: utf-8 -*-*-
#
# Copyright (C) 2005-2009 University of Deusto
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.
#
# This software consists of contributions made by many individuals, 
# listed below:
#
# Author: <NAME> <<EMAIL>>
#

import cgi
import urllib
import SocketServer
import BaseHTTPServer

import weblab.facade.RemoteFacadeServer as RFS
from weblab.facade.RemoteFacadeServer import strdate

from weblab.facade.RemoteFacadeContext import get_context, create_context, delete_context

import voodoo.log as log

class MethodException(Exception):
    def __init__(self, status, msg):
        super(MethodException, self).__init__((status, msg))
        self.status = status
        self.msg    = msg

class Method(object):

    path = ""

    def __init__(self, request_handler, cfg_manager, server):
        self.req         = request_handler
        self.cfg_manager = cfg_manager
        self.server      = server
        self.post_read   = False

    def run(self):
        return "Method %s does not implement run method!" % self.__class__.__name__

    def get_argument(self, name, default_value = None, avoid_post = False):
        for arg_name, value in self.get_arguments():
            if arg_name == name:
                return value
        if not avoid_post:
            self.read_post_arguments()
            postvar = self.postvars.get(name, None)
            if postvar is None:
                return default_value
            if len(postvar) == 0:
                return default_value
            return postvar[0]
        return default_value

    def get_arguments(self):
        if self.relative_path.find('?') < 0:
            return []
        query = self.relative_path[self.relative_path.find('?') + 1:]
        return [ (arg[:arg.find('=')], arg[arg.find('=')+1:]) for arg in query.split('&') if arg.find('=') > 0]


    def raise_exc(self, status, message):
        raise MethodException(status, message)

    def get_context(self):
        return get_context()

    @property
    def relative_path(self):
        return Method.get_relative_path(self.req.path)

    @property
    def weblab_cookie(self):
        return self.req.weblab_cookie

    @property
    def uri(self):
        return self.req.path.split('?')[0]

    @property
    def method(self):
        return self.req.command

    def read_post_arguments(self):
        if self.post_read:
            return # Already read
        self.post_read = True
        if self.method == 'POST':
            ctype, pdict = cgi.parse_header(self.req.headers.getheader('content-type'))
            if ctype == 'multipart/form-data':
                self.postvars = cgi.parse_multipart(self.req.rfile, pdict)
            elif ctype == 'application/x-www-form-urlencoded':
                length = int(self.req.headers.getheader('content-length'))
                self.postvars = cgi.parse_qs(self.req.rfile.read(length), keep_blank_values=1)
            else:
                self.postvars = {}
        else:
            self.postvars = {}

    @staticmethod
    def get_relative_path(path):
        # If coming from /weblab001/web/login/?foo=bar will return
        # /login/?foo=bar
        # 
        # If coming from /foo/?bar will erturn
        # /foo/?bar
        finder = '/web/'
        if path.find(finder) >= 0:
            return path[path.find(finder) + len(finder) - 1:]
        return path

    @classmethod
    def matches(klass, absolute_path):
        relative_path = Method.get_relative_path(absolute_path)
        return relative_path.startswith(klass.path)

class NotFoundMethod(Method):
    def run(self):
        self.raise_exc(404, "Path %s not found!" % urllib.quote(self.req.path))

class WebHttpHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    methods         = None
    server_route    = None
    cfg_manager     = None
    original_server = None

    def do_GET(self):
        if self.server_route is None:
            route = self.server_route
            self.weblab_cookie = "weblabsessionid=anythinglikeasessid.%s" % route
        else:
            self.weblab_cookie = ""

        create_context(self.server, self.headers)<commit_msg>[web facade facebook]  * Fixing a cookies-related issue<commit_after>#!/usr/bin/env python
#-*-*- encoding: utf-8 -*-*-
#
# Copyright (C) 2005-2009 University of Deusto
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.
#
# This software consists of contributions made by many individuals, 
# listed below:
#
# Author: <NAME> <<EMAIL>>
#

import cgi
import urllib
import SocketServer
import BaseHTTPServer

import weblab.facade.RemoteFacadeServer as RFS
from weblab.facade.RemoteFacadeServer import strdate

from weblab.facade.RemoteFacadeContext import get_context, create_context, delete_context

import voodoo.log as log

class MethodException(Exception):
    def __init__(self, status, msg):
        super(MethodException, self).__init__((status, msg))
        self.status = status
        self.msg    = msg

class Method(object):

    path = ""

    def __init__(self, request_handler, cfg_manager, server):
        self.req         = request_handler
        self.cfg_manager = cfg_manager
        self.server      = server
        self.post_read   = False

    def run(self):
        return "Method %s does not implement run method!" % self.__class__.__name__

    def get_argument(self, name, default_value = None, avoid_post = False):
        for arg_name, value in self.get_arguments():
            if arg_name == name:
                return value
        if not avoid_post:
            self.read_post_arguments()
            postvar = self.postvars.get(name, None)
            if postvar is None:
                return default_value
            if len(postvar) == 0:
                return default_value
            return postvar[0]
        return default_value

    def get_arguments(self):
        if self.relative_path.find('?') < 0:
            return []
        query = self.relative_path[self.relative_path.find('?') + 1:]
        return [ (arg[:arg.find('=')], arg[arg.find('=')+1:]) for arg in query.split('&') if arg.find('=') > 0]


    def raise_exc(self, status, message):
        raise MethodException(status, message)

    def get_context(self):
        return get_context()

    @property
    def relative_path(self):
        return Method.get_relative_path(self.req.path)

    @property
    def weblab_cookie(self):
        return self.req.weblab_cookie

    @property
    def uri(self):
        return self.req.path.split('?')[0]

    @property
    def method(self):
        return self.req.command

    def read_post_arguments(self):
        if self.post_read:
            return # Already read
        self.post_read = True
        if self.method == 'POST':
            ctype, pdict = cgi.parse_header(self.req.headers.getheader('content-type'))
            if ctype == 'multipart/form-data':
                self.postvars = cgi.parse_multipart(self.req.rfile, pdict)
            elif ctype == 'application/x-www-form-urlencoded':
                length = int(self.req.headers.getheader('content-length'))
                self.postvars = cgi.parse_qs(self.req.rfile.read(length), keep_blank_values=1)
            else:
                self.postvars = {}
        else:
            self.postvars = {}

    @staticmethod
    def get_relative_path(path):
        # If coming from /weblab001/web/login/?foo=bar will return
        # /login/?foo=bar
        # 
        # If coming from /foo/?bar will erturn
        # /foo/?bar
        finder = '/web/'
        if path.find(finder) >= 0:
            return path[path.find(finder) + len(finder) - 1:]
        return path

    @classmethod
    def matches(klass, absolute_path):
        relative_path = Method.get_relative_path(absolute_path)
        return relative_path.startswith(klass.path)

class NotFoundMethod(Method):
    def run(self):
        self.raise_exc(404, "Path %s not found!" % urllib.quote(self.req.path))

class WebHttpHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    methods         = None
    server_route    = None
    cfg_manager     = None
    original_server = None

    def do_GET(self):
        if self.server_route is not None:
            route = self.server_route
            self.weblab_cookie = "weblabsessionid=anythinglikeasessid.%s" % route
        else:
            self.weblab_cookie = "weblabsessionid=sessid.not.found"

        create_context(self.server, self.headers)
{'fraction_non_alphanumeric': 0.104348826507133, 'fraction_numerical': 0.005752416014726185, 'mean_word_length': 2.402348336594912, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6877923', 'n_tokens_mistral': 2663, 'n_tokens_neox': 2561, 'n_words': 619}