From 46736549d33893ccf8419bc9f7dab8002430b12b Mon Sep 17 00:00:00 2001 From: Kload Date: Sat, 15 Jun 2013 19:57:12 +0000 Subject: [PATCH] Add pattern matching to url params --- .dynette.rb.swp | Bin 12288 -> 12288 bytes dynette.rb | 29 +++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/.dynette.rb.swp b/.dynette.rb.swp index 53281720b723be3a525553da0084b666e486f7d5..7b135451caaea5b8bda0c61702af4b21c7528f57 100644 GIT binary patch delta 841 zcmb8sO=uHA6bJB$mDbvpG-yET!J&z?`Pj`iNtJ}g2nsQ%P*KDZ*JP62(jm!q-4AU{ zmh|GKc-ymHDkxr50*WFPJa`e09u(}&Q;#0&!GBk*rBuXWf3w5xdo%N9mMhDZU{$(W zyhu-qDS|di$hY@zF3mj;Zu)(*y};;q|A^R6$jZR4pR;a&cDoxRgsj6C_zWN61*}3H zu0bDc4ioYYo&pC3>qCTmfV<#82Nalq!|?VXAunMGR2YIjxHm}1925pgfuu2bb%2m( za38L~1vm!<$bcVyVB25e6Rg2wcnGVoGSKtwPxbrGx(}jbz1Pt-Uqs}(NoQtgQ5S25 z$}Dv<%k?r-6ockYQA53HtEPpZNKcDPK6gGwk6gB^%3_mojj{$(loqvEUDdMggfQzq zi60w_5Q-L}#rf?Bb%~pnNn92?-aW6jC^sqBZm|YevNY7znZcT7DO@IBwqWc?p3d9 z3ZZU2b~O6uBKAH`^Y=x`ZbqQPI}gtd#1D<{UPzGJsrb|hC!FvW3ehv}BS8vy3*r_k Qw-**!lUGP@LkRBs2{iTKApigX delta 250 zcmZojXh;xEG6?hZRWR2xW&i>K28Pg`djf+uivH$jwBIZ!@R^^tjgx_49v4K?ezKrK zef=w528KI8d>x3d0r4py-VDUEfH)e6J%IQf4+Fz9AkGKkSRnQS;`7`L499?YGZ1G1 zaS{;60dXu4`vb8m5DNkEXP|Abf%pm#?*rmpK)e%(cL4ErE(Uh6bLMhwW@PE$oZO`@ zwRw^H0mjW!HPsk7l@&t#UHq*Sk|(dyH4srw%}ZfW00QNN@{Ih%+{^?9<;|~kMVSDX C6gBt& diff --git a/dynette.rb b/dynette.rb index 8360705..842804d 100755 --- a/dynette.rb +++ b/dynette.rb @@ -28,18 +28,37 @@ class Ip belongs_to :entry end -#get '/' do - #`whoami` -#end +get '/' do + `whoami` +end post '/' do content_type :json - # TODO: check params + # Check params + unless params[:subdomain].match /^[a-z0-9-]{3,16}$/ + status 400 + return { :error => "Subdomain is invalid: #{params[:subdomain]}.#{DOMAIN}" } + end + unless params[:public_key].match /^[a-z0-9]{22}==$/i + status 400 + return { :error => "Key is invalid: #{params[:public_key]}" } + end + unless params[:current_ip].match /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/ + status 400 + return { :error => "Key is invalid: #{params[:current_ip]}" } + end + + # If already exists + if entry = Entry.first(:subdomain => params[:subdomain]) + status 409 + return { :error => "Subdomain already taken: #{entry.subdomain}.#{DOMAIN}" } + end if entry = Entry.first(:public_key => params[:public_key]) status 409 return { :error => "Key already exists for domain #{entry.subdomain}.#{DOMAIN}" } end entry = Entry.new(:public_key => params[:public_key], :subdomain => params[:subdomain], :current_ip => request.ip) + entry.ips << Ip.create(:ip_addr => request.ip) if entry.save status 201 return { :public_key => params[:public_key], :subdomain => params[:subdomain], :current_ip => request.ip }.to_json @@ -58,6 +77,4 @@ get '/all' do Entry.all.to_json end - - DataMapper.auto_upgrade!