summary refs log tree commit diff homepage
path: root/src/http.cr
blob: 9562c2e02b766adf93fbe6cd4c6e4542965deaa1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# HTTP server
# Copyright (C) 2023  Nguyễn Gia Phong
#
# This file if part of hybring.
#
# Hybring is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Hybring is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with hybring.  If not, see <https://www.gnu.org/licenses/>.

require "http/server"
require "ini"
require "uri"

require "./sqlite"
require "./xhtml"

HTML_HEADINGS = Set{"applicants", "criteria", "joining", "members"}
MAX_CONTENT_LENGTH = 4096
MAX_NICK_LENGTH = 32
NICK_PATTERN = "[0-9a-z]{1,#{MAX_NICK_LENGTH}}"
OPENNIC_TLD = Set{".bbs", ".chan", ".cyb", ".dyn", ".epic",
                  ".geek", ".gopher", ".indy", ".libre", ".neo",
                  ".null", ".o", ".oss", ".oz", ".parody", ".pirate"}

def http_error(context, status, message = nil)
  context.response.respond_with_status status, message
end

class Server
  def initialize(cfg)
    @db = Database.new cfg.db, cfg.opennic_remote, cfg.icann_remote

    @opennic_host = URI.parse(cfg.opennic_remote).host
    @opennic_page = Page.new cfg.opennic_local, cfg.opennic_remote, cfg.api, @db
    @opennic_page.write
    @icann_page = Page.new cfg.icann_local, cfg.icann_remote, cfg.api, @db
    @icann_page.write

    @server = HTTP::Server.new do |context|
      # Manually crafted request
      next http_error context, 405 if context.request.method != "POST"
      content_length = context.request.content_length
      next http_error context, 411 unless content_length
      next http_error context, 413 if content_length > MAX_CONTENT_LENGTH
      content_type = context.request.headers["Content-Type"]?
      unless content_type && content_type == "application/x-www-form-urlencoded"
        next http_error context, 415
      end

      errors = {} of String => String
      params = {} of String => String
      invalid_param = false
      body = context.request.body.try &.gets_to_end || ""
      URI::Params.parse body do |key, value|
        params[key] = value
        case key
        when "nick"
          if /^#{NICK_PATTERN}$/ !~ value
            # Manually crafted request or non-mainstream browser engine
            next errors["nick"] = "Must match #{NICK_PATTERN}"
          end
        when "opennic"
          uri = URI.parse value
          next errors["opennic"] = "Must be absolute URL" unless uri.absolute?
          if uri.scheme != "http" && uri.scheme != "https"
            next errors["opennic"] = "Must be HTTP/S"
          end
          unless OPENNIC_TLD.includes? Path[uri.host.not_nil!].extension
            next errors["opennic"] = "Must be under OpenNIC domain"
          end
        when "icann"
          uri = URI.parse value
          next errors["icann"] = "Must be absolute URL" unless uri.absolute?
          next errors["icann"] = "Must be HTTPS" unless uri.scheme == "https"
          # impractical to check for ICANN TLD
          if OPENNIC_TLD.includes? Path[uri.host.not_nil!].extension
            next errors["icann"] = "Must not be under OpenNIC domain"
          end
        when "host"
        else
          break invalid_param = true
        end
      end

      # Manually crafted request
      next http_error context, 400, "Invalid Parameter" if invalid_param
      next http_error context, 400, "Missing Parameter" unless params.size == 4

      @db.exec "SELECT count(CASE WHEN nick = %Q THEN 1 END),
                       count(CASE WHEN opennic = %Q THEN 1 END),
                       count(CASE WHEN icann = %Q THEN 1 END)
                FROM member",
               params["nick"], params["opennic"], params["icann"] do |row|
        errors["nick"] = "Must be unique" if row[0].int > 0
        errors["opennic"] = "Must be unique" if row[1].int > 0
        errors["icann"] = "Must be unique" if row[2].int > 0
      end
      if HTML_HEADINGS.includes? params["nick"]
        errors["nick"] = "Reserved names: #{HTML_HEADINGS.join ", "}"
      end

      if errors.empty?
        @db.add_applicant params["nick"], params["opennic"], params["icann"]
        # TODO: write feed
      else
        context.response.status_code = 400
      end
      context.response.content_type = "application/xhtml+xml"
      if params["host"] == @opennic_host
        context.response.print @opennic_page.build errors, params
      else
        context.response.print @icann_page.build errors, params
      end
      # TODO: schedule dynamic check
    end
  end

  getter opennic_page
  getter icann_page

  def listen(port = 0)
    @db.update_hook ->(arg : Void*, action : Database::UpdateAction,
                       db : LibC::Char*, table : LibC::Char*, rowid : Int64) {
      return unless db == "main"
      obj = arg.as Server
      case table
      when "member"
        # FIXME: query in a different conn
        obj.opennic_page.write
        obj.icann_page.write
      end
    }, self.as Void*

    yield @server.bind_tcp port
    @server.listen
  end

  def listening?
    @server.listening?
  end

  def close
    @server.close
  end
end