summary refs log tree commit diff homepage
path: root/src/http.cr
diff options
context:
space:
mode:
Diffstat (limited to 'src/http.cr')
-rw-r--r--src/http.cr18
1 files changed, 15 insertions, 3 deletions
diff --git a/src/http.cr b/src/http.cr
index 912a46c..248a746 100644
--- a/src/http.cr
+++ b/src/http.cr
@@ -50,6 +50,10 @@ class Server
       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
@@ -60,7 +64,7 @@ class Server
         case key
         when "nick"
           if value.size > MAX_NICK_LENGTH
-            next errors["nick"] = "Must be within ${MAX_NICK_LENGTH} characters"
+            next errors["nick"] = "Must be within #{MAX_NICK_LENGTH} characters"
           end
           if /^[0-9a-z]+$/ !~ value
             next errors["nick"] = "Must be ASCII lowercase alphanumeric"
@@ -124,7 +128,7 @@ class Server
   getter opennic_page
   getter icann_page
 
-  def listen(port)
+  def listen(port = 0)
     @db.update_hook ->(arg : Void*, action : Database::UpdateAction,
                        db : LibC::Char*, table : LibC::Char*, rowid : Int64) {
       return unless db == "main"
@@ -136,7 +140,15 @@ class Server
       end
     }, self.as Void*
 
-    puts "Listening on http://#{@server.bind_tcp port}"
+    yield @server.bind_tcp port
     @server.listen
   end
+
+  def listening?
+    @server.listening?
+  end
+
+  def close
+    @server.close
+  end
 end