require 'sinatra' require "json" require "json/add/core" require "P4" require "p4if" configure do set :bind, 'rest4p.das.perforce.com' set :port, 1999 end VALID_FORMS = Array[ 'branch', 'change', 'client', 'counter', 'depot', 'group', 'job', 'key', 'label', 'stream', 'user'] before do cache_control :must_revalidate if request.path != "/_session" and request.path != "/env" cookie = request.cookies['PUP'] if !cookie halt 401, "Session not initialized" end end if (request.accept? 'text/plain') content_type 'text/plain' else content_type 'application/json' end end get '/_session' do content_type 'text/html' erb :session end delete '/_session' do response.delete_cookie 'PUP' body "Session deleted" end post '/_session' do charset = nil if params[:unicode] charset = "utf8" end result = p4_login( params[:port], params[:user], params[:pass], charset ) if result.length == 1 response.delete_cookie 'PUP' halt 401, result[0] end if result[0][0][0] == "'" response.delete_cookie 'PUP' halt 401, "Account must have a password set" end response.set_cookie 'PUP', result[0] body JSON.pretty_generate( result[1] ) end ['/p4/*s', '/p4/*S'].each do |path| get path do |form| form.downcase! if !VALID_FORMS.include?(form) and form != 'branche' halt 400, "Invalid or unsupport form: #{form}s" end cookie = request.cookies['PUP'] args = p4_args( request.env["rack.request.query_hash"] ) result = p4_cmd( cookie, "#{form}s", args ) if result.length == 1 halt 400, result[0] end body JSON.pretty_generate( result[1] ) end end get '/p4/*/*' do |form, name| form.downcase! if !VALID_FORMS.include?(form) halt 400, "Invalid or unsupport form: #{form}" end if name.length == 0 halt 400, "Invalid request" end cookie = request.cookies['PUP'] args = p4_args( request.env["rack.request.query_hash"] ) args << "-o" args << name result = p4_cmd( cookie, "#{form}", args ) if result.length == 1 halt 400, result[0] end body JSON.pretty_generate( result[1] ) end get '/env' do a = Array.new a << request.accept # ['text/html', '*/*'] a << (request.accept? 'text/xml') # true a << request.body # request body sent by the client (see below) a << request.scheme # "http" a << request.script_name # "/example" a << request.path_info # "/foo" a << request.port # 80 a << request.request_method # "GET" a << request.query_string # "" a << request.content_length # length of request.body a << request.media_type # media type of request.body a << request.host # "example.com" a << request.get? # true (similar methods for other verbs) a << request.form_data? # false a << request["some_param"] # value of some_param parameter. [] is a shortcut to the params hash. a << request.referrer # the referrer of the client or '/' a << request.user_agent # user agent (used by :agent condition) a << request.cookies # hash of browser cookies a << request.xhr? # is this an ajax request? a << request.url # "http://example.com/example/foo" a << request.path # "/example/foo" a << request.ip # client IP address a << request.secure? # false (would be true over ssl) a << request.forwarded? # true (if running behind a reverse proxy) a << request.env # raw env hash handed in by Rack JSON.pretty_generate( a ) end