#!/usr/bin/env python3
# Single purpose HTTP server
# - serves files specified as arguments in order of appearance


import os
import sys
import http.server

class Handler(http.server.BaseHTTPRequestHandler):
    def do_POST(self):
        data = self.rfile.read(int(self.headers.get('content-length')))

        try:
            attachment = data.splitlines()[4:-1]
        except Exception as ex:
            print(str(ex))
            self.reply_false(400)
            return

        if len(attachment) != 1:
            self.reply_false(400)
            return

        try:
            value = next(Handler.expected_iter)
            print('Expected', value)
            print('Received', attachment[0])
            if value != attachment[0]:
                self.reply_false(400)
        except StopIteration:
            self.reply_false(500)

        self.reply_true(202)

    def reply_true(self, response_code):
        self.send_response(response_code)
        self.send_header("Content-type", "application/json")
        self.end_headers()
        self.wfile.write(b'{"result": true}')

    def reply_false(self, response_code):
        self.send_response(response_code)
        self.send_header("Content-type", "application/json")
        self.end_headers()
        self.wfile.write(b'{"result": false}')


PORT = 12345
print("Serving at port", PORT)

Handler.expected_iter = [
b'{ "bthash": "072c77ed630343ca99dd5333cfd159a3b062e69b", "type": "RHBZ", "data": "851210" }',
b'{ "bthash": "072c77ed630343ca99dd5333cfd159a3b062e69b", "type": "RHBZ", "data": "12345" }',
b'{ "bthash": "DEADBEAF", "type": "RHBZ", "data": "851210" }',
b'{ "bthash": "DEADBEAF", "type": "RHBZ", "data": "12345" }',
b'{ "bthash": "072c77ed630343ca99dd5333cfd159a3b062e69b", "type": "email", "data": "argument@redhat.com" }',
b'{ "bthash": "072c77ed630343ca99dd5333cfd159a3b062e69b", "type": "email", "data": "environment@redhta.com" }',
b'{ "bthash": "DEADBEAF", "type": "email", "data": "argument@redhat.com" }',
b'{ "bthash": "DEADBEAF", "type": "email", "data": "environment@redhta.com" }',
b'{ "bthash": "072c77ed630343ca99dd5333cfd159a3b062e69b", "type": "comment", "data": "command line user comment" }',
b'{ "bthash": "072c77ed630343ca99dd5333cfd159a3b062e69b", "type": "comment", "data": "dir user comment" }',
b'{ "bthash": "DEADBEAF", "type": "comment", "data": "command line user comment" }',
b'{ "bthash": "DEADBEAF", "type": "comment", "data": "dir user comment" }',
b'{ "bthash": "072c77ed630343ca99dd5333cfd159a3b062e69b", "type": "url1", "data": "http:\/\/bugzilla.redhat.com\/1000000" }',
b'{ "bthash": "DEADBEAF", "type": "url2", "data": "http:\/\/bugzilla.redhat.com\/1000001" }',
b'{ "bthash": "072c77ed630343ca99dd5333cfd159a3b062e69b", "type": "url3", "data": "https:\/\/bugzilla.redhat.com\/show_bug.cgi?id=851210" }',
b'{ "bthash": "DEADBEAF", "type": "url4", "data": "https:\/\/bugzilla.redhat.com\/show_bug.cgi?id=851210" }',
].__iter__()

httpd = http.server.HTTPServer(("", PORT), Handler)
httpd.serve_forever()
