gloo-web 1.5 → 1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f65c05593ecaa1245da14193d91d7302b3c831dc211e4e2ff3803d4c923159c7
4
- data.tar.gz: 85d932c0ceca6e2108076a5e243ea4b927e2e605a36f90444d81646c023212d4
3
+ metadata.gz: f6d3032841d8b93dd380ad4d6663d49ecd09e643e9367e4bd692b77bc553bf15
4
+ data.tar.gz: b93f0d2eb10d68f534efafc93e9309fcb17082a8c32225f2e84a7132d4ce19a3
5
5
  SHA512:
6
- metadata.gz: 48c826d58586853dee37b4a616748847f7111bb5f4fe4afc90d2ca56f052dd4d3c45b76ec10e5ced598b6051bd2ab6770da49fecd0da569df9d832a693a76296
7
- data.tar.gz: 44df546269144ffcaac4efe96404ab061ecf4f2dfd49fefb4c3dd2add2904702dfa51e5fe69ad40a5c0051365fd4d9c95a5a8c185b49deb1dca925e98b773aee
6
+ metadata.gz: 93c67f1b0883db8f204d2cc623a7393fbc58f328ca946446348727434be72171916b8dbbbd48b781e730c05689e6a929c52f93565908134baf37f5d99ace773d
7
+ data.tar.gz: db469e4e91276218949244391269bd9b9c390f6c0b0c6ea524932796a45250182f3f4e7207c9646a9ab1e1296e4f8968fd0bc28e26612dc4fc9d9e56593a8260
data/lib/objs/element.rb CHANGED
@@ -169,7 +169,7 @@ module Objs
169
169
  end
170
170
 
171
171
  #
172
- # Get the expiration date for the certificate.
172
+ # Render the element as HTML and put it in it.
173
173
  #
174
174
  def msg_render
175
175
  content = self.render_html
@@ -239,11 +239,15 @@ module Objs
239
239
  return rendered_obj_content
240
240
  end
241
241
 
242
+ #
243
+ # Render a string or other object; an unexpected failure is
244
+ # handled and renders as blank.
245
+ #
242
246
  def self.render_thing e, render_ƒ, engine
243
247
  begin
244
248
  return e.render( render_ƒ )
245
249
  rescue => e
246
- engine.log_exception e
250
+ engine.handle_exception e
247
251
  return ''
248
252
  end
249
253
  end
@@ -265,7 +269,7 @@ module Objs
265
269
  'content (container) — The element contents.'
266
270
  ],
267
271
  :messages => [
268
- 'render — Manually render the HTML element. Normally the render is called by the web server when the page containing the element is requested.'
272
+ 'render — Manually render the HTML element; the HTML is put in it. Normally the render is called by the web server when the page containing the element is requested.'
269
273
  ],
270
274
  :examples => <<~EXAMPLES.strip
271
275
  div_sub [e] :
data/lib/objs/field.rb CHANGED
@@ -453,7 +453,7 @@ module Objs
453
453
  'checked (boolean) — Optional; only used for checkbox fields. Whether the checkbox field is checked.'
454
454
  ],
455
455
  :messages => [
456
- 'render — Manually render the HTML field. Normally the render is called by the web server when the page containing the element is requested.'
456
+ 'render — Manually render the HTML field; the HTML is put in it. Normally the render is called by the web server when the page containing the element is requested.'
457
457
  ],
458
458
  :examples => <<~EXAMPLES.strip
459
459
  f [form] :
data/lib/objs/form.rb CHANGED
@@ -260,7 +260,7 @@ module Objs
260
260
  begin
261
261
  return e.render( 'render_html' )
262
262
  rescue => e
263
- @engine.log_exception e
263
+ @engine.handle_exception e
264
264
  return ''
265
265
  end
266
266
  end
@@ -286,7 +286,7 @@ module Objs
286
286
  'content (container) — The collection of form fields. Other elements can also be included here, for formatting or extra buttons.'
287
287
  ],
288
288
  :messages => [
289
- 'render — Manually render the HTML form. Normally the render is called by the web server when the page containing the element is requested.'
289
+ 'render — Manually render the HTML form; the HTML is put in it. Normally the render is called by the web server when the page containing the element is requested.'
290
290
  ],
291
291
  :examples => <<~EXAMPLES.strip
292
292
  page [can] :
data/lib/objs/page.rb CHANGED
@@ -314,14 +314,30 @@ module Objs
314
314
  end
315
315
 
316
316
  #
317
- # Get the expiration date for the certificate.
317
+ # Render the page and put the content (HTML, JSON or text) in it,
318
+ # or false if it can't render. Only works inside a running web app.
318
319
  #
319
320
  def msg_render
320
- content = self.render
321
- @engine.heap.it.set_to content
321
+ return nil unless Page.can_render?( @engine, self )
322
+
323
+ response = self.render
324
+ content = response&.data
325
+ @engine.heap.it.set_to( content.nil? ? false : content )
322
326
  return content
323
327
  end
324
328
 
329
+ #
330
+ # Is a web app running, so the object can render? If not, report
331
+ # it and put false in it. Shared with partial.
332
+ #
333
+ def self.can_render?( engine, obj )
334
+ return true if engine.app_running?
335
+
336
+ engine.err "#{obj.type_display} '#{obj.name}' can only render inside a web app (gloo-web)."
337
+ engine.heap.it.set_to false
338
+ return false
339
+ end
340
+
325
341
 
326
342
  # ---------------------------------------------------------------------
327
343
  # Render
@@ -584,7 +600,7 @@ module Objs
584
600
  'download_file (boolean) — Optional, default false. If true, the browser is instructed to download the file instead of rendering it.'
585
601
  ],
586
602
  :messages => [
587
- 'render — Manually render the content of the page. Normally the render is called by the web server when the page is requested.'
603
+ 'render — Manually render the content of the page; the content (HTML, JSON or text) is put in it, or false if it can\'t render. Only works inside a running web app (it is an error otherwise). Normally the render is called by the web server when the page is requested.'
588
604
  ],
589
605
  :examples => <<~EXAMPLES.strip
590
606
  page [can] :
data/lib/objs/partial.rb CHANGED
@@ -140,11 +140,14 @@ module Objs
140
140
  end
141
141
 
142
142
  #
143
- # Get the expiration date for the certificate.
143
+ # Render the partial and put the HTML in it, or false if it can't
144
+ # render. Only works inside a running web app.
144
145
  #
145
146
  def msg_render
147
+ return nil unless Page.can_render?( @engine, self )
148
+
146
149
  part_content = self.render
147
- @engine.heap.it.set_to part_content
150
+ @engine.heap.it.set_to( part_content.nil? ? false : part_content )
148
151
  return part_content
149
152
  end
150
153
 
@@ -226,7 +229,7 @@ module Objs
226
229
  'content (element) — The contents of the partial page.'
227
230
  ],
228
231
  :messages => [
229
- 'render — Manually render the content of the partial page. Normally the render is called by the web server when the page containing the partial is requested.'
232
+ 'render — Manually render the content of the partial page; the HTML is put in it, or false if it can\'t render. Only works inside a running web app (it is an error otherwise). Normally the render is called by the web server when the page containing the partial is requested.'
230
233
  ],
231
234
  :examples => <<~EXAMPLES.strip
232
235
  shared [can] :
data/lib/objs/svr.rb CHANGED
@@ -58,9 +58,10 @@ module Objs
58
58
  # Default layout for pages.
59
59
  LAYOUT = 'layout'.freeze
60
60
 
61
- # Alias to the home and error pages
61
+ # Alias to the home, error and not found pages
62
62
  HOME = 'home'.freeze
63
63
  ERR_PAGE = 'error'.freeze
64
+ NOT_FOUND_PAGE = 'not_found'.freeze
64
65
 
65
66
  # Session
66
67
  SESSION = 'session'.freeze
@@ -68,6 +69,9 @@ module Objs
68
69
 
69
70
  # Messages
70
71
  SERVER_NOT_RUNNING = 'The web server is not running!'.freeze
72
+ NOT_IN_SCRIPT_MODE_ERR = "Could not start the web server: an app runs in App mode. Run it with 'gloo --app %s'.".freeze
73
+ UNKNOWN_APP_FOLDER = '<folder>'.freeze
74
+ SERVER_ALREADY_RUNNING = 'The web server is already running!'.freeze
71
75
 
72
76
  #
73
77
  # Should the current request be redirected?
@@ -123,13 +127,20 @@ module Objs
123
127
  # Configuration
124
128
  # ---------------------------------------------------------------------
125
129
 
130
+ #
131
+ # Get the config child with the given name.
132
+ # Returns nil if there is none, or no config container.
133
+ #
134
+ def config_child( name )
135
+ return find_child( CONFIG )&.find_child( name )
136
+ end
137
+
126
138
  #
127
139
  # Get the Scheme (http or https) from the child object.
128
140
  # Returns nil if there is none.
129
141
  #
130
142
  def scheme_value
131
- config = find_child CONFIG
132
- scheme = config.find_child SCHEME
143
+ scheme = config_child SCHEME
133
144
  return nil unless scheme
134
145
 
135
146
  return scheme.value
@@ -140,8 +151,7 @@ module Objs
140
151
  # Returns nil if there is none.
141
152
  #
142
153
  def host_value
143
- config = find_child CONFIG
144
- host = config.find_child HOST
154
+ host = config_child HOST
145
155
  return nil unless host
146
156
 
147
157
  return host.value
@@ -152,8 +162,7 @@ module Objs
152
162
  # Returns nil if there is none.
153
163
  #
154
164
  def port_value
155
- config = find_child CONFIG
156
- port = config.find_child PORT
165
+ port = config_child PORT
157
166
  return nil unless port
158
167
 
159
168
  return port.value
@@ -171,8 +180,7 @@ module Objs
171
180
  # Get the session cookie name.
172
181
  #
173
182
  def session_name
174
- config = find_child CONFIG
175
- session_name = config.find_child SESSION_NAME
183
+ session_name = config_child SESSION_NAME
176
184
  return nil unless session_name
177
185
 
178
186
  name = session_name.value
@@ -185,8 +193,7 @@ module Objs
185
193
  # Get the key for the encryption cipher.
186
194
  #
187
195
  def encryption_key
188
- config = find_child CONFIG
189
- o = config.find_child ENCRYPT_KEY
196
+ o = config_child ENCRYPT_KEY
190
197
  return nil unless o
191
198
 
192
199
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
@@ -197,8 +204,7 @@ module Objs
197
204
  # Get the initialization vector for the cipher.
198
205
  #
199
206
  def encryption_iv
200
- config = find_child CONFIG
201
- o = config.find_child ENCRYPT_IV
207
+ o = config_child ENCRYPT_IV
202
208
  return nil unless o
203
209
 
204
210
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
@@ -210,8 +216,7 @@ module Objs
210
216
  # If not specified, use the root path.
211
217
  #
212
218
  def session_cookie_path
213
- config = find_child CONFIG
214
- o = config.find_child COOKIE_PATH
219
+ o = config_child COOKIE_PATH
215
220
  if o
216
221
  return o.value
217
222
  else
@@ -224,8 +229,7 @@ module Objs
224
229
  # If not specified, use one week from now.
225
230
  #
226
231
  def session_cookie_expires
227
- config = find_child CONFIG
228
- o = config.find_child COOKIE_EXPIRES
232
+ o = config_child COOKIE_EXPIRES
229
233
  if o
230
234
  dt = Chronic.parse( o.value )
231
235
  return dt
@@ -239,7 +243,7 @@ module Objs
239
243
  # Get the value from the scheme settings/config.
240
244
  #
241
245
  def session_cookie_secure
242
- return scheme_value.downcase == HTTPS
246
+ return scheme_value&.downcase == HTTPS
243
247
  end
244
248
 
245
249
 
@@ -392,6 +396,7 @@ module Objs
392
396
  fac.create_alias LAYOUT, nil, self
393
397
  fac.create_alias HOME, nil, self
394
398
  fac.create_alias ERR_PAGE, nil, self
399
+ fac.create_alias NOT_FOUND_PAGE, nil, self
395
400
 
396
401
  fac.create_can PAGES, self
397
402
  end
@@ -415,6 +420,13 @@ module Objs
415
420
  # Start the gloo web server.
416
421
  #
417
422
  def msg_start
423
+ return script_mode_err if @engine.mode == Gloo::App::Mode::SCRIPT
424
+
425
+ if @engine.app_running?
426
+ @engine.err SERVER_ALREADY_RUNNING
427
+ return
428
+ end
429
+
418
430
  @engine.log.debug "Starting web server…"
419
431
  # @engine.log.quiet = true
420
432
 
@@ -423,6 +435,26 @@ module Objs
423
435
  # The running app will call the start function (below)
424
436
  end
425
437
 
438
+ #
439
+ # A web server is an app, and an app runs in App mode. Started from
440
+ # a script run (gloo folder/start.gloo) it would stop as soon as the
441
+ # script finished: report the mistake, with the command that runs
442
+ # the app, instead of starting.
443
+ #
444
+ def script_mode_err
445
+ @engine.err format( NOT_IN_SCRIPT_MODE_ERR, app_folder )
446
+ end
447
+
448
+ #
449
+ # The folder of the file gloo was run with (in Script mode, the
450
+ # first file loaded): the folder to give --app. A placeholder if
451
+ # nothing came from a file.
452
+ #
453
+ def app_folder
454
+ pn = @engine.persist_man.maps.first&.pn
455
+ return pn ? File.dirname( pn ) : UNKNOWN_APP_FOLDER
456
+ end
457
+
426
458
  #
427
459
  # Stop the running web server.
428
460
  #
@@ -522,12 +554,14 @@ module Objs
522
554
  # Start running the web server.
523
555
  #
524
556
  def start
525
- config = WebSvr::Config.new( scheme_value, host_value, port_value )
557
+ # Settings that aren't given use the defaults.
558
+ config = WebSvr::Config.new( scheme_value || WebSvr::Config::HTTP,
559
+ host_value || WebSvr::Config::LOCALHOST,
560
+ port_value || WebSvr::Config::PORT_DEFAULT )
526
561
  @engine.log.info "Web Server URL: #{config.base_url}"
527
562
 
528
563
  handler = WebSvr::Handler.new( @engine, self )
529
564
  @web_server = WebSvr::Server.new( @engine, handler, config, ssl_config )
530
- @web_server.start
531
565
 
532
566
  @router = Routing::Router.new( @engine, self )
533
567
  @router.add_page_routes
@@ -540,6 +574,10 @@ module Objs
540
574
  @session = WebSvr::Session.new( @engine, self )
541
575
 
542
576
  run_on_start
577
+
578
+ # Listen last: if the server can't start, it stops the running
579
+ # app from its own thread, so everything else must be done.
580
+ @web_server.start
543
581
  @engine.log.info "Web server started and listening…"
544
582
  end
545
583
 
@@ -658,7 +696,7 @@ module Objs
658
696
  data.find_child( PAGE )&.set_value( page_obj.pn )
659
697
  end
660
698
  rescue => e
661
- @engine.log_exception e
699
+ @engine.handle_exception e
662
700
  end
663
701
  end
664
702
 
@@ -696,6 +734,17 @@ module Objs
696
734
  return o
697
735
  end
698
736
 
737
+ #
738
+ # Get the application not found page.
739
+ #
740
+ def not_found_page
741
+ o = find_child NOT_FOUND_PAGE
742
+ return nil unless o
743
+
744
+ o = Gloo::Objs::Alias.resolve_alias( @engine, o )
745
+ return o
746
+ end
747
+
699
748
  #
700
749
  # Get the default layout for pages.
701
750
  #
@@ -720,12 +769,13 @@ module Objs
720
769
  :shortcut => KEYWORD_SHORT,
721
770
  :description => 'A web server running inside gloo.',
722
771
  :children => [
723
- 'config (container) — Configuration and settings for the server. See below for its children.',
772
+ 'config (container) — Configuration and settings for the server. See below for its children. Settings left out use the defaults.',
724
773
  'on_start (script) — Run when the web server is started.',
725
774
  'on_stop (script) — Run when the web server is stopped.',
726
775
  "layout (partial) — By convention an alias pointing to the layout used for all pages (layouts live in the layout root-level folder).",
727
776
  'home (page) — By convention an alias pointing to the home page object (pages live in the page root-level folder).',
728
- 'error (page) — By convention an alias pointing to the default error page object.',
777
+ 'error (page) — By convention an alias pointing to the default error page object. Sent with a 500 when a request fails; without one, a plain "Server error!" is sent.',
778
+ 'not_found (page) — By convention an alias pointing to the page sent with a 404 when no route or file matches the request; without one, a plain "Not found" is sent.',
729
779
  'pages (container) — Routes. By convention, aliases pointing to pages in the page root-level folder.',
730
780
  'config.scheme (string) — \'http\' or \'https\'.',
731
781
  "config.host (string) — Default: 'localhost'.",
@@ -735,16 +785,18 @@ module Objs
735
785
  'config.encryption_iv (string) — Optional. Initialization vector for the session cookie encryption key.',
736
786
  "config.cookie_expires (string) — Optional, default 'in 1 week'. When the session expires.",
737
787
  "config.cookie_path (string) — Optional, default '/'. The path for the session cookie.",
738
- 'config.ssl_cert (string) — Optional, required for SSL. Path to certificate.pem.',
739
- 'config.ssl_key (string) — Optional, required for SSL. Path to key.pem.',
788
+ 'ssl_cert (string or file) — Optional, required for SSL; a child of the server, not of config. Path to certificate.pem. May be an alias.',
789
+ 'ssl_key (string or file) — Optional, required for SSL; a child of the server, not of config. Path to key.pem. May be an alias.',
740
790
  'on_request (script) — Optional. Run when the web server receives a request.',
741
791
  'request_data (container) — Optional. Data available for use in on_request or elsewhere in the page rendering life-cycle: method, host, path, query, ip (all string, all optional, populated only if present).',
742
792
  'on_response (script) — Optional. Run when the web server is done rendering and about to return a response.',
743
793
  'response_data (container) — Optional. Data available for use in on_response (request_data is also available at this point): page, type, code, elapsed, db (all string, all optional, populated only if present).'
744
794
  ],
745
795
  :messages => [
746
- 'start — Start the web server.',
747
- 'stop — Stop the web server.',
796
+ 'start — Start the web server. It is an error if a web server is already running. If it can\'t listen on its port (in use, or needs root privileges), that is reported as an error and the server stops. ' \
797
+ 'A web server is an app and runs in App mode (gloo --app {folder}): started from a script run (gloo {folder}/start.gloo) it does not start, and reports ' \
798
+ "Could not start the web server: an app runs in App mode. Run it with 'gloo --app {folder}'.",
799
+ 'stop — Stop the web server. It is an error if it is not running.',
748
800
  'list_routes — Show the routing table. A debugging tool.',
749
801
  'list_assets — Show the list of assets. A debugging tool.',
750
802
  'list_asset_img — Show the list of image assets. A debugging tool.',
@@ -773,6 +825,7 @@ module Objs
773
825
  layout [alias] : layout.primary
774
826
  home [alias] : page.home
775
827
  error [alias] : page.err
828
+ not_found [alias] : page.not_found
776
829
 
777
830
  # Routes for the web server.
778
831
  pages [container] :
@@ -98,7 +98,7 @@ module WebSvr
98
98
  #
99
99
  # Handle a missing method by looking for a helper function.
100
100
  # If there is one, then call it and return the result.
101
- # If not, log an error and return nil.
101
+ # If not, report an error and return nil (it renders as blank).
102
102
  #
103
103
  def method_missing( method_name, *args )
104
104
  @log.debug "missing method '#{method_name}' with args #{args}"
@@ -108,11 +108,13 @@ module WebSvr
108
108
 
109
109
  pn = Gloo::Core::Pn.new( @engine, helper_pn )
110
110
  obj = pn.resolve
111
- if obj
111
+ if obj.nil?
112
+ @engine.err "Helper function '#{method_name}' was not found; add it as #{helper_pn}."
113
+ elsif !obj.respond_to?( :invoke )
114
+ @engine.err "'#{helper_pn}' is not a function."
115
+ else
112
116
  @log.debug "found obj: #{obj.pn}"
113
117
  return obj.invoke args
114
- else
115
- @log.error "Function not found: #{helper_pn}"
116
118
  end
117
119
 
118
120
  return nil
@@ -7,7 +7,10 @@
7
7
 
8
8
  module WebSvr
9
9
  class Handler
10
-
10
+
11
+ SERVER_ERR_MSG = 'Server error!'.freeze
12
+ NOT_FOUND_MSG = 'Not found'.freeze
13
+
11
14
  attr_reader :server_obj
12
15
 
13
16
 
@@ -55,7 +58,7 @@ module WebSvr
55
58
  page_obj = page
56
59
  end
57
60
  else
58
- result = server_error_result
61
+ result = not_found_result
59
62
  end
60
63
 
61
64
  return result, page_obj
@@ -87,7 +90,7 @@ module WebSvr
87
90
 
88
91
  # Check to make sure it is a valid file
89
92
  # return error if it is not
90
- return file_error_result unless File.exist? pn
93
+ return not_found_result unless File.exist? pn
91
94
 
92
95
  return @server_obj.asset.render_file pn
93
96
  end
@@ -98,25 +101,37 @@ module WebSvr
98
101
  # ---------------------------------------------------------------------
99
102
 
100
103
  #
101
- # Return a server error result.
102
- # Use the app's error if there is one, otherwise a generic message.
104
+ # Return a server error result: the app's error page, or a generic
105
+ # message, sent as a 500.
103
106
  #
104
107
  def server_error_result
105
- err_page = @server_obj.err_page
106
- return err_page.render if err_page
108
+ return page_or_text_result( @server_obj.err_page,
109
+ SERVER_ERR_MSG, WebSvr::ResponseCode::SERVER_ERR )
110
+ end
107
111
 
108
- # Last resort, just return a generic error message.
109
- return WebSvr::Response.text_response( @engine,
110
- "Server error!", WebSvr::ResponseCode::SERVER_ERR )
111
- end
112
-
113
112
  #
114
- # Get a file not found error result.
113
+ # Return a not found result: the app's not found page, or a generic
114
+ # message, sent as a 404.
115
115
  #
116
- def file_error_result
117
- return WebSvr::Response.text_response( @engine,
118
- "File not found!", WebSvr::ResponseCode::NOT_FOUND )
119
- end
116
+ def not_found_result
117
+ return page_or_text_result( @server_obj.not_found_page,
118
+ NOT_FOUND_MSG, WebSvr::ResponseCode::NOT_FOUND )
119
+ end
120
+
121
+ #
122
+ # Render the given page with the given status code, whatever the
123
+ # page's own return code. Without a page, or if it doesn't render,
124
+ # send the message as text.
125
+ #
126
+ def page_or_text_result( page, msg, code )
127
+ result = page.render if page.is_a?( Objs::Page )
128
+ if result
129
+ result.code = code
130
+ return result
131
+ end
132
+
133
+ return WebSvr::Response.text_response( @engine, msg, code )
134
+ end
120
135
 
121
136
 
122
137
  # ---------------------------------------------------------------------
@@ -53,6 +53,9 @@ module WebSvr
53
53
  begin
54
54
  @body_params = Rack::Utils.parse_query body
55
55
  rescue => exception
56
+ # A binary body (a file upload) isn't a query string and
57
+ # fails to parse; read it as multipart instead. Any problem
58
+ # with that is reported there.
56
59
  init_multipart body
57
60
  end
58
61
  else
@@ -101,8 +104,7 @@ module WebSvr
101
104
  @body_params[ 'file_size' ] = binary_data.length
102
105
  @body_params[ 'file_data' ] = binary_data
103
106
  rescue => ex
104
- @engine.log_exception ex
105
- # @log.error "Error parsing multipart request", ex
107
+ @engine.err "Could not read the uploaded file: #{ex.message}"
106
108
  end
107
109
  end
108
110
 
@@ -16,7 +16,8 @@ module WebSvr
16
16
  JSON_TYPE = 'application/json'.freeze
17
17
  HTML_TYPE = 'text/html'.freeze
18
18
 
19
- attr_reader :code, :type, :data
19
+ attr_reader :type, :data
20
+ attr_accessor :code
20
21
  attr_accessor :location
21
22
  attr_accessor :file_name
22
23
 
@@ -25,7 +25,11 @@ require 'rack'
25
25
 
26
26
  module WebSvr
27
27
  class Server
28
-
28
+
29
+ # EventMachine (under Thin) raises a RuntimeError starting with this
30
+ # when it can't listen: the port is in use or needs root.
31
+ NO_ACCEPTOR = 'no acceptor'.freeze
32
+
29
33
  # ---------------------------------------------------------------------
30
34
  # Initialization
31
35
  # ---------------------------------------------------------------------
@@ -52,20 +56,34 @@ module WebSvr
52
56
  # Start the web server.
53
57
  #
54
58
  def start
59
+ @server_thread = Thread.new { run_server }
60
+ @log.debug 'Web server has started.'
61
+ end
62
+
63
+ #
64
+ # Run the web server. This doesn't return while the server is
65
+ # running, so it runs in its own thread. If the server fails, it is
66
+ # reported here and the running app is stopped.
67
+ #
68
+ def run_server
55
69
  opts = {
56
70
  :Port => @config.port,
57
71
  :Host => @config.host
58
72
  }
59
- Thread.abort_on_exception = true
60
- @server_thread = Thread.new {
61
- Rack::Handler::Thin.run( self, **options=opts ) do |server|
62
- if @ssl_config
63
- server.ssl = true
64
- server.ssl_options = @ssl_config
65
- end
73
+ Rack::Handler::Thin.run( self, **opts ) do |server|
74
+ if @ssl_config
75
+ server.ssl = true
76
+ server.ssl_options = @ssl_config
66
77
  end
67
- }
68
- @log.debug 'Web server has started.'
78
+ end
79
+ rescue => e
80
+ if e.message.start_with?( NO_ACCEPTOR )
81
+ @engine.err "Could not start the web server on #{@config.host}:#{@config.port}: " \
82
+ 'the port is in use or needs root privileges.'
83
+ else
84
+ @engine.handle_exception e
85
+ end
86
+ @engine.stop_running_app
69
87
  end
70
88
 
71
89
  #
@@ -74,7 +92,8 @@ module WebSvr
74
92
  def stop
75
93
  @log.debug 'Stopping the web server…'
76
94
 
77
- @server_thread.kill
95
+ # When the server thread itself failed, it is ending already.
96
+ @server_thread.kill unless Thread.current == @server_thread
78
97
 
79
98
  @log.debug 'The web server has been stopped.'
80
99
  end
@@ -92,9 +111,31 @@ module WebSvr
92
111
  request.log
93
112
 
94
113
  response = request.process
95
- response.log if response
96
114
 
97
- return response ? response.result : nil
115
+ # A page that failed to render gives no response.
116
+ response ||= @handler.server_error_result
117
+ response.log
118
+
119
+ return response.result
120
+ rescue => e
121
+ # A bug in gloo-web's own code. Errors in gloo statements are
122
+ # handled where they happen, so this is the unexpected case:
123
+ # report it and send the error page.
124
+ @engine.handle_exception e
125
+ return error_result
126
+ end
127
+
128
+ #
129
+ # The result for a request that failed: the app's error page, or a
130
+ # plain 500 if that fails too.
131
+ #
132
+ def error_result
133
+ return @handler.server_error_result.result
134
+ rescue => e
135
+ @engine.handle_exception e
136
+ return [ WebSvr::ResponseCode::SERVER_ERR,
137
+ { WebSvr::Response::CONTENT_TYPE => WebSvr::Response::TEXT_TYPE },
138
+ WebSvr::Handler::SERVER_ERR_MSG ]
98
139
  end
99
140
 
100
141
 
@@ -9,12 +9,14 @@
9
9
  # https://en.wikipedia.org/wiki/HTTP_cookie
10
10
  #
11
11
  require 'base64'
12
+ require 'openssl'
12
13
 
13
14
  module WebSvr
14
15
  class Session
15
16
 
16
17
  SESSION_CONTAINER = 'session'.freeze
17
18
  SESSION_ID_NAME = 'session_id'.freeze
19
+ SESSION_UNREADABLE = 'Session cookie could not be read; starting a new session.'.freeze
18
20
 
19
21
 
20
22
  # ---------------------------------------------------------------------
@@ -64,7 +66,8 @@ module WebSvr
64
66
  end
65
67
  end
66
68
  rescue => e
67
- @engine.log_exception e
69
+ # Not expected: a bad cookie is handled in decode_decrypt.
70
+ @engine.handle_exception e
68
71
  end
69
72
  end
70
73
 
@@ -166,8 +169,10 @@ module WebSvr
166
169
  begin
167
170
  data = Gloo::Objs::Cipher.decrypt( data, key, iv )
168
171
  return JSON.parse( data )
169
- rescue => e
170
- @engine.log_exception e
172
+ rescue OpenSSL::Cipher::CipherError, JSON::ParserError
173
+ # The cookie was made with another key, or has been changed.
174
+ # Not the app's error: the visitor gets a new session.
175
+ @engine.warn SESSION_UNREADABLE
171
176
  return nil
172
177
  end
173
178
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gloo-web
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.5'
4
+ version: '1.7'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Crane
@@ -9,6 +9,20 @@ bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: gloo
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '7.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '7.0'
12
26
  - !ruby/object:Gem::Dependency
13
27
  name: thin
14
28
  requirement: !ruby/object:Gem::Requirement