<commit_before>"""
REST API for connections.

Author: <NAME> <<EMAIL>>
Copyright: NORDUnet (2015)
"""

import time
import json

from twisted.python import log
from twisted.internet import defer
from twisted.web import resource, server

from opennsa import nsa, error, state, constants as cnt, database
from opennsa.shared import xmlhelper
from opennsa.protocols.shared import requestauthz
from opennsa.protocols.nsi2 import helper



LOG_SYSTEM='protocol.rest'
RN = '\r\n'

CONTENT_LENGTH = 'content-length' # twisted.web doesn't have this as a constant

# Connection Fields
START_TIME = 'start_time'
END_TIME = 'end_time'

COMMIT    = b'COMMIT'
ABORT     = b'ABORT'
PROVISION = b'PROVISION'
RELEASE   = b'RELEASE'
TERMINATE = b'TERMINATE'

ACTIONS = [ COMMIT, ABORT, PROVISION, RELEASE, TERMINATE ]


def _requestResponse(request, code, payload, headers=None):
    # helper
    request.setResponseCode(code)
    request.setHeader(CONTENT_LENGTH, len(payload))
    if headers is not None:
        for key, value in headers.items():
            request.setHeader(key, value)
    return payload


def _finishRequest(request, code, payload, headers=None):
    # helper

    # make it work with string and bytes (we do expect string here, but it is not enforced)
    if type(payload) is str:
        payload = payload.encode()

    _requestResponse(request, code, payload, headers)
    request.write(payload)
    request.finish()


def _errorCode(ex):
    if isinstance(ex, error.NSIError):
        return 400 # Client Error
    else:
        return 500 # Server Error


def _createErrorResponse(err, request):
    log.msg('%s while creating connection: %s' % (str(err.type), str(err.value)), system=LOG_SYSTEM)
<commit_msg>fix issue with twisted 19 not accepting ints as header value<commit_after>"""
REST API for connections.

Author: <NAME> <<EMAIL>>
Copyright: NORDUnet (2015)
"""

import time
import json

from twisted.python import log
from twisted.internet import defer
from twisted.web import resource, server

from opennsa import nsa, error, state, constants as cnt, database
from opennsa.shared import xmlhelper
from opennsa.protocols.shared import requestauthz
from opennsa.protocols.nsi2 import helper



LOG_SYSTEM='protocol.rest'
RN = '\r\n'

CONTENT_LENGTH = 'content-length' # twisted.web doesn't have this as a constant

# Connection Fields
START_TIME = 'start_time'
END_TIME = 'end_time'

COMMIT    = b'COMMIT'
ABORT     = b'ABORT'
PROVISION = b'PROVISION'
RELEASE   = b'RELEASE'
TERMINATE = b'TERMINATE'

ACTIONS = [ COMMIT, ABORT, PROVISION, RELEASE, TERMINATE ]


def _requestResponse(request, code, payload, headers=None):
    # helper
    request.setResponseCode(code)
    request.setHeader(CONTENT_LENGTH, str(len(payload)))
    if headers is not None:
        for key, value in headers.items():
            request.setHeader(key, value)
    return payload


def _finishRequest(request, code, payload, headers=None):
    # helper

    # make it work with string and bytes (we do expect string here, but it is not enforced)
    if type(payload) is str:
        payload = payload.encode()

    _requestResponse(request, code, payload, headers)
    request.write(payload)
    request.finish()


def _errorCode(ex):
    if isinstance(ex, error.NSIError):
        return 400 # Client Error
    else:
        return 500 # Server Error


def _createErrorResponse(err, request):
    log.msg('%s while creating connection: %s' % (str(err.type), str(err.value)), system=LOG_SYSTEM)

{'fraction_non_alphanumeric': 0.0979381443298969, 'fraction_numerical': 0.006872852233676976, 'mean_word_length': 3.9971387696709586, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '1192532', 'n_tokens_mistral': 1120, 'n_tokens_neox': 1067, 'n_words': 344}