From 40c7e0f9aa2fc0d1353818e5316da2203d114cf0 Mon Sep 17 00:00:00 2001 From: Steve Coast Date: Thu, 24 May 2007 11:20:16 +0000 Subject: [PATCH] apply patch from TomH for #477 and dont require http auth on GET to the API. --- app/controllers/application.rb | 50 ++++++++++++++++++---------------- lib/daemons/gpx_import.rb | 25 +++++++++++++---- 2 files changed, 45 insertions(+), 30 deletions(-) diff --git a/app/controllers/application.rb b/app/controllers/application.rb index fd6ea7866..52aeff3cb 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -11,31 +11,33 @@ class ApplicationController < ActionController::Base end def authorize(realm='Web Password', errormessage="Couldn't authenticate you") - username, passwd = get_auth_data # parse from headers - # authenticate per-scheme - if username.nil? - @user = nil # no authentication provided - perhaps first connect (client should retry after 401) - elsif username == 'token' - @user = User.authenticate_token(passwd) # preferred - random token for user from db, passed in basic auth - else - @user = User.authenticate(username, passwd) # basic auth + unless request.get? + username, passwd = get_auth_data # parse from headers + # authenticate per-scheme + if username.nil? + @user = nil # no authentication provided - perhaps first connect (client should retry after 401) + elsif username == 'token' + @user = User.authenticate_token(passwd) # preferred - random token for user from db, passed in basic auth + else + @user = User.authenticate(username, passwd) # basic auth + end + + # handle authenticate pass/fail + if @user + # user exists and password is correct ... horray! + if @user.methods.include? 'lastlogin' # note last login + @session['lastlogin'] = user.lastlogin + @user.last.login = Time.now + @user.save() + @session["User.id"] = @user.id + end + else + # no auth, the user does not exist or the password was wrong + response.headers["Status"] = "Unauthorized" + response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\"" + render_text(errormessage, 401) # :unauthorized + end end - - # handle authenticate pass/fail - if @user - # user exists and password is correct ... horray! - if @user.methods.include? 'lastlogin' # note last login - @session['lastlogin'] = user.lastlogin - @user.last.login = Time.now - @user.save() - @session["User.id"] = @user.id - end - else - # no auth, the user does not exist or the password was wrong - response.headers["Status"] = "Unauthorized" - response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\"" - render_text(errormessage, 401) # :unauthorized - end end # Report and error to the user diff --git a/lib/daemons/gpx_import.rb b/lib/daemons/gpx_import.rb index 82232e496..acf215300 100755 --- a/lib/daemons/gpx_import.rb +++ b/lib/daemons/gpx_import.rb @@ -23,17 +23,26 @@ while($running) do begin logger.info("GPX Import importing #{trace.name} (#{trace.id}) from #{trace.user.email}") - + # TODO *nix specific, could do to work on windows... would be functionally inferior though - check for '.gz' - gzipped = `file -b /home/osm/gpx/#{trace.id}.gpx`.chomp =~/^gzip/ + filetype = `file -b /home/osm/gpx/#{trace.id}.gpx`.chomp + gzipped = filetype =~ /^gzip/ + zipped = filetype =~ /^Zip/ if gzipped logger.info("gzipped") + filename = "/tmp/#{rand}" + system("gunzip -c /home/osm/gpx/#{trace.id}.gpx > #{filename}") + elsif zipped + logger.info("zipped") + filename = "/tmp/#{rand}" + system("unzip -p /home/osm/gpx/#{trace.id}.gpx > #{filename}") else logger.info("not gzipped") + filename = "/home/osm/gpx/#{trace.id}.gpx" end - gpx = OSM::GPXImporter.new("/home/osm/gpx/#{trace.id}.gpx") + gpx = OSM::GPXImporter.new(filename) f_lat = 0 f_lon = 0 @@ -74,10 +83,14 @@ while($running) do trace.inserted = true trace.save + if gzipped || zipped + FileUtils.rm_f(filename) + end + logger.info "done trace #{trace.id}" Notifier::deliver_gpx_success(trace, gpx.possible_points) else - `rm /home/osm/gpx/#{trace.id}.gpx` + FileUtils.rm_f("/home/osm/gpx/#{trace.id}.gpx", filename) trace.destroy Notifier::deliver_gpx_failure(trace, '0 points parsed ok. Do they all have lat,lng,alt,timestamp?') end @@ -85,7 +98,7 @@ while($running) do rescue Exception => ex logger.info ex ex.backtrace.each {|l| logger.info l } - `rm /home/osm/gpx/#{trace.id}.gpx` + FileUtils.rm_f("/home/osm/gpx/#{trace.id}.gpx", filename) trace.destroy Notifier::deliver_gpx_failure(trace, ex.to_s + ex.backtrace.join("\n") ) end @@ -93,7 +106,7 @@ while($running) do end Trace.find(:all, :conditions => ['inserted = ?', false]).each do |trace| - `rm /home/osm/gpx/#{trace.id}.gpx` + FileUtils.rm_f("/home/osm/gpx/#{trace.id}.gpx") trace.destroy end exit -- 2.43.2