<commit_before># -*- coding: utf-8 -*-
# Copyright 2015, 2016 OpenMarket Ltd
# Copyright 2017 New Vector Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging

import six

from prometheus_client import Counter

from twisted.internet import defer
from twisted.internet.error import AlreadyCalled, AlreadyCancelled

from synapse.metrics.background_process_metrics import run_as_background_process
from synapse.push import PusherConfigException

from . import push_rule_evaluator, push_tools

if six.PY3:
    long = int

logger = logging.getLogger(__name__)

http_push_processed_counter = Counter(
    "synapse_http_httppusher_http_pushes_processed",
    "Number of push notifications successfully sent",
)

http_push_failed_counter = Counter(
    "synapse_http_httppusher_http_pushes_failed",
    "Number of push notifications which failed",
)

http_badges_processed_counter = Counter(
    "synapse_http_httppusher_badge_updates_processed",
    "Number of badge updates successfully sent",
)

http_badges_failed_counter = Counter(
    "synapse_http_httppusher_badge_updates_failed",
    "Number of badge updates which failed",
)


class HttpPusher(object):
    INITIAL_BACKOFF_SEC = 1  # in seconds because that's what Twisted takes
    MAX_BACKOFF_SEC = 60 * 60

    # This one's in ms because we compare it against the clock
    GIVE_UP_AFTER_MS = 24 * 60 * 60 * 1000

    def __init__(self, hs, pusherdict):
        self.hs = hs
        self.store = self.hs.get_datastore()
        self.clock = self.hs.get_clock()
        self.state_handler = self.hs.get_state_handler()
        self.user_id = pusherdict['user_name']
        self.app_id = pusherdict['app_id']
        self.app_display_name = pusherdict['app_display_name']
        self.device_display_name = pusherdict['device_display_name']
        self.pushkey = pusherdict['pushkey']
        self.pushkey_ts = pusherdict['ts']
        self.data = pusherdict['data']
        self.last_stream_ordering = pusherdict['last_stream_ordering']
        self.backoff_delay = HttpPusher.INITIAL_BACKOFF_SEC
        self.failing_since = pusherdict['failing_since']
        self.timed_call = None
        self._is_processing = False

        # This is the highest stream ordering we know it's safe to process.
        # When new events arrive, we'll be given a window of new events: we
        # should honour this rather than just looking for anything higher
        # because of potential out-of-order event serialisation. This starts
        # off as None though as we don't know any better.
        self.max_stream_ordering = None

        if 'data' not in pusherdict:
            raise PusherConfigException(
                "No 'data' key for HTTP pusher"
            )
        self.data = pusherdict['data']

        self.name = "%s/%s/%s" % (
            pusherdict['user_name'],
            pusherdict['app_id'],
            pusherdict['pushkey'],
        )

        if 'url' not in self.data:
            raise PusherConfigException(
                "'url' required in data for HTTP pusher"
            )
        self.url = self.data['url']<commit_msg>Correctly handle null data in HttpPusher<commit_after># -*- coding: utf-8 -*-
# Copyright 2015, 2016 OpenMarket Ltd
# Copyright 2017 New Vector Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging

import six

from prometheus_client import Counter

from twisted.internet import defer
from twisted.internet.error import AlreadyCalled, AlreadyCancelled

from synapse.metrics.background_process_metrics import run_as_background_process
from synapse.push import PusherConfigException

from . import push_rule_evaluator, push_tools

if six.PY3:
    long = int

logger = logging.getLogger(__name__)

http_push_processed_counter = Counter(
    "synapse_http_httppusher_http_pushes_processed",
    "Number of push notifications successfully sent",
)

http_push_failed_counter = Counter(
    "synapse_http_httppusher_http_pushes_failed",
    "Number of push notifications which failed",
)

http_badges_processed_counter = Counter(
    "synapse_http_httppusher_badge_updates_processed",
    "Number of badge updates successfully sent",
)

http_badges_failed_counter = Counter(
    "synapse_http_httppusher_badge_updates_failed",
    "Number of badge updates which failed",
)


class HttpPusher(object):
    INITIAL_BACKOFF_SEC = 1  # in seconds because that's what Twisted takes
    MAX_BACKOFF_SEC = 60 * 60

    # This one's in ms because we compare it against the clock
    GIVE_UP_AFTER_MS = 24 * 60 * 60 * 1000

    def __init__(self, hs, pusherdict):
        self.hs = hs
        self.store = self.hs.get_datastore()
        self.clock = self.hs.get_clock()
        self.state_handler = self.hs.get_state_handler()
        self.user_id = pusherdict['user_name']
        self.app_id = pusherdict['app_id']
        self.app_display_name = pusherdict['app_display_name']
        self.device_display_name = pusherdict['device_display_name']
        self.pushkey = pusherdict['pushkey']
        self.pushkey_ts = pusherdict['ts']
        self.data = pusherdict['data']
        self.last_stream_ordering = pusherdict['last_stream_ordering']
        self.backoff_delay = HttpPusher.INITIAL_BACKOFF_SEC
        self.failing_since = pusherdict['failing_since']
        self.timed_call = None
        self._is_processing = False

        # This is the highest stream ordering we know it's safe to process.
        # When new events arrive, we'll be given a window of new events: we
        # should honour this rather than just looking for anything higher
        # because of potential out-of-order event serialisation. This starts
        # off as None though as we don't know any better.
        self.max_stream_ordering = None

        if 'data' not in pusherdict:
            raise PusherConfigException(
                "No 'data' key for HTTP pusher"
            )
        self.data = pusherdict['data']

        self.name = "%s/%s/%s" % (
            pusherdict['user_name'],
            pusherdict['app_id'],
            pusherdict['pushkey'],
        )

        if self.data is None:
            raise PusherConfigException(
                "data can not be null for HTTP pusher"
            )

        if 'url' not in self.data:
            raise PusherConfigException(
                "'url' required in data for HTTP pusher"
            )
        self.url = self.data['url']
{'fraction_non_alphanumeric': 0.0761956228046474, 'fraction_numerical': 0.008916509051607674, 'mean_word_length': 3.105934553521908, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2103599', 'n_tokens_mistral': 2221, 'n_tokens_neox': 2115, 'n_words': 656}