Class: RWDServer

Inherits:
Object show all
Defined in:
lib/rwd/rwd.rb

Instance Method Summary collapse

Constructor Details

#initialize(obj, port, io, auth, realm) ⇒ RWDServer

Returns a new instance of RWDServer.



1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
# File 'lib/rwd/rwd.rb', line 1164

def initialize(obj, port, io, auth, realm)
  @object		= obj
  @localbrowsing	= false
  @browserstarted	= false
  @sessions		= Sessions.new(obj.class, (not auth.nil?))

  if auth.nil?
    @localbrowsing	= true

    if ENV.include?("RWDBROWSER") and not ENV["RWDBROWSER"].nil? and not ENV["RWDBROWSER"].empty?
      @browserstarted	= true
      @object.exitbrowser

	# Start browser.

      @browserthread	=
      Thread.new do
        puts "Starting the browser..."

        #if ENV["RWDBROWSER"].downcase =~ /iexplore/	# ???
          #@ie	= IE.new("http://localhost:#{port}/")
        #else
          browser	= ENV["RWDBROWSER"].dup
          url		= "http://localhost:%s/" % [port]

          re		= /[$%]1\b/
          command	= "%s \"%s\"" % [browser, url]
          command	= browser.gsub(re, url)	if browser =~ re

          command.gsub!(/%port%/, port.to_s)

          system(command) or $stderr.puts "Starting of the browser failed, or the browser terminated abnormally.\nCommand => #{command}"
        #end

        puts "The browser has terminated."
      end
    end
  end

	# Start server.

  portio		= port
  portio		= [port, io]	unless io.nil?
  threadlimiter	= ThreadLimiter.new(1)

  HTTPServer.serve(portio, (not auth.nil?)) do |req, resp|
    threadlimiter.wait do
      vars	= {}
      req.vars.each do |k, v|
        vars[k]	= v.join("\t")
      end
      pad	= (req.request.path or "/")

      if auth.kind_of? String
        file	= "#{home}/#{auth}"
        auths	= {}
        auths	= Hash.file(file)	if File.file?(file)
      else
        auths	= auth
      end

      #oldsessionid	= vars["rwd_session"]
      oldsessionid	= req.cookies["sessionid"]

# Retrieve session.

      session	= @sessions[oldsessionid]

# Eventually create new session.

      if session.nil?
        sessionid	= MD5.new(req.peeraddr[3].to_s + @object.inspect.to_s + ("%1.6f" % Time.now.to_f)).to_s	while (sessionid == nil or @sessions.include?(sessionid))
        session		= RWDSession.new(sessionid)

        if auth.nil?
          session["object"]		= @object
        else
          session["object"]		= @object.clone
        end

        if oldsessionid.nil? or oldsessionid.empty?
          if not auth.nil? and not auth.empty? and not session.authenticated and pad !~ /^\/rwd_/

# Check authentication

            us	= vars["rwd_a"]
            pa	= vars["rwd_b"]

            if us.nil? or pa.nil? or auths[us] != pa
              session				= RWDSession.new
              session["object"]		= RWDLogin.new(realm)
              pad				= "/"
            else
              session.authenticated		= true
              @sessions[session.sessionid]	= session
            end
          else
            session.authenticated		= true
            @sessions[session.sessionid]	= session
          end
        else
          session		= RWDSession.new
          session["object"]	= RWDTimeOut.new
        end

        vars	= {}
      end

# Avoid timeout.

      session.touch

      if pad == "/"

# Serve methods/callbacks.

# Build new page.

        download	= ""
        downloadfile	= ""
        res		= ""

        done	= session.render(res, pad, vars, download, downloadfile, req.pda?)

        begin
          if download.empty?
            resp["Content-Type"]		= "text/html"
            if done
              resp.cookies["sessionid"]	= ""
            else
              resp.cookies["sessionid"]	= session.sessionid
            end

            resp << res
          else
            resp["Content-Type"]		= "application/octet-stream"
            resp["Content-Disposition"]	= "attachment;"
            resp["Content-Disposition"]	= "attachment; filename=%s" % downloadfile	unless downloadfile.empty?

            resp << download
          end
        rescue
          puts "Sending response to browser failed."

          @sessions.delete(session.sessionid)
        end

# Eventually delete this session.

        if done
          @sessions.delete(session.sessionid)

          if @localbrowsing
            resp.stop

            if @browserstarted and not @browserthread.nil? and @browserthread.alive?
              resp.stop do
                puts "Waiting for the browser to terminate..."

                @browserthread.join
              end
            end
          end
        end

      else

# Serve files.

        if pad == "/rwd_pixel.gif"
          resp["Cache-Control"]	= "max-age=86400"
          resp["Content-Type"]	= "image/gif"
          resp << $rwd_pixel
        else
          if session.authenticated
            pwd	= Dir.pwd
            file	= File.expand_path(pad.gsub(/^\/*/, ""), $rwd_files)

            if not file.index(pwd) == 0
              resp["Content-Type"]	= "text/html"
              resp.response		= "HTTP/1.0 400 BAD REQUEST"
              resp << "<html><body><p><b>Bad Request.</b> (<tt>#{pad}</tt>)</p></body></html>"
            elsif File.file?(file)
              resp << File.new(file, "rb").read	rescue nil
            else
              resp["Content-Type"]	= "text/html"
              resp.response		= "HTTP/1.0 404 NOT FOUND"
              resp << "<html><body><p><b>Not found.</b> (<tt>#{pad}</tt>)</p></body></html>"
            end
          else
            resp["Content-Type"]	= "text/html"
            resp.response		= "HTTP/1.0 ??? NOT AUTHORIZED"
            resp << "<html><body><p><b>Not Authorized.</b></p></body></html>"
          end
        end

      end

    end
  end
end