Streaming Example From "Sinatra: Up and Running" Not Working - sinatra

I am trying to run this example from the book Sinatra: Up and Running p. 46, but can't get it to work. Here's the program code:
require 'sinatra'
before do
content_type :txt
end
connections = []
get '/consume' do
stream(:keep_open) do |out|
# store connection for later on
connections << out
# remove connection when closed properly
out.callback { connections.delete(out) }
# remove connection when closed due to an error
out.errback do
logger.warn 'we just lost a connection!'
connections.delete(out)
end
end
end
get '/broadcast/:message' do
connections.each do |out|
out << "#{Time.now} -> #{params[:message]}" << "\n"
end
"Sent #{params[:message]} to all clients."
end
The instructions for testing the code are as follows:
It’s a little tricky to demonstrate the behavior in text, but a good demonstration would
be to start the application, then open a web browser and navigate to http://localhost:
4567/consume. Next, open a terminal and use cURL to send messages to the server.
$ curl http://localhost:4567/broadcast/hello
Sent hello to all clients.
If you look back at the web browser, you should see that the content of the page has
been updated with a time stamp and the message that you sent via the terminal. The
connection remains open, and the client continues to wait for further information from
the server.
When I follow these instruction, I get no errors, but the message "hello" does not appear in the browser. I am running Sinatra on with Webrick. Why is it not working?
Thanks!
UPDATE (Konstantin's Thin Suggestion)
I now start thin and perform the two steps described in the book and the OP. You can see that thin does indeed receive both requests. However, I am still not seeeing the output "hello" in the browser.
>rackup
>> Thin web server (v1.4.1 codename Chromeo)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:9292, CTRL+C to stop
127.0.0.1 - - [13/Aug/2012 12:48:03] "GET /consume HTTP/1.1" 200 - 0.0900
127.0.0.1 - - [13/Aug/2012 12:48:03] "GET /favicon.ico HTTP/1.1" 404 447 0.0000
127.0.0.1 - - [13/Aug/2012 12:49:02] "GET /broadcast/hello HTTP/1.1" 200 26 0.00
00
127.0.0.1 - - [13/Aug/2012 12:57:00] "GET /consume HTTP/1.1" 200 - 0.0000
Perhaps the mistake is in my configu.ru file:
require './streaming.rb'
run Sinatra::Application

Run Sinatra on Thin. :keep_open is not supported on Webrick. Make sure you're running Sinatra 1.3.3 or later.

I had the same problem. To speed up the response, I used
before do
content_type 'text/event-stream'
end

The second route has to be a POST:
post '/broadcast/:message' do
connections.each do |out|
out << "#{Time.now} -> #{params[:message]}" << "\n"
end
"Sent #{params[:message]} to all clients."
end
After that, you will also have to POST your message to the server:
curl -vX POST 127.0.0.1:4567/broadcast/hello

Related

HAProxy cuts parameters in log

Good day everyone!
I’m migrated from haproxy 1.5 to 1.7.11 and I have some troubles with logging
I have a following in config file for logging
capture request header Host len 200
capture request header Referer len 200
capture request header User-Agent len 200
capture request header Content-Type len 200
capture request header Cookie len 300
log-format %[capture.req.hdr(0),lower]\ %ci\ -\ [%t]\ \"%HM\ %HP\ %HV\"\ %ST\ \"%[capture.req.hdr(3)]\"\ %U\ \"%[capture.req.hdr(1)]\"\ \"%[capture.req.hdr(2)]\"\ \"%[capture.req.hdr(4)]\"\ %Tq\ \"%s\"\ 'NGINX-CACHE-- "-"'\ \"%ts\»
Logformat is almost the same with Nginx
But is some cases it works incorrectly
For example log output
Nov 20 10:41:56 lb.loc haproxy[12633]: example.com 81.4.227.173 - [20/Nov/2019:10:41:56.095] "GET /piwik.php H" 200 "-" 2396 "https://example.com/" "Mozilla/5.0" "some.cookie data" 19 "vm06.lb.loc" NGINX-CACHE-- "-" "—"
Problem is that "GET /piwik.php H" must be "GET /piwik.php HTTP/1.1"
its %HV parameter in log-format
A part of "HTTP/1.1" randomly cut’s off. It may be "HT" or "HTT" or "HTTP/1."
I think we have discussed this on the HAProxy mailing list.
https://www.mail-archive.com/haproxy#formilux.org/msg35426.html
There are some bug fixes in the buffer handling therefore please try to update to the latest 1.7.
As you mentioned on the HAProxy list that you use CentOS 6 and you use the packages from ius repo please install 1.7.12 which is listed on the page below.
https://repo.ius.io/6/x86_64/packages/h/
As described in documentation:
req.hdr(): [...] The function considers any comma as a delimiter for distinct values. If full-line headers are desired instead, use req.fhdr(). [...]
So, you should use req.fhdr() to have the full header value.
For example, like this:
http-request capture req.fhdr(User-Agent) len 256k
Information from issue thread in official repository.

Serving two (ipython generated) reveal slide shows at once

I'd like to be able to have two sets of slides, produced from two different notebooks, open at once in my browser. This use case is not supported, as far as I can tell, by the option --post serve of ipython nbconvert --to slides (of course, I'd be happy to be disproved).
My tactic has been to start a local server, as in
python -m SimpleHTTPServer 8001
and open the slide shows like this
google-chrome http://127.0.0.1:8001/my.slides.html
but now I get a bunch of messages alike
127.0.0.1 - - [31/Mar/2015 12:03:49] code 404, message File not found
127.0.0.1 - - [31/Mar/2015 12:03:49] "GET /reveal.js/css/reveal.css HTTP/1.1" 404 -
whose meaning is quite clear to me... so I did
ln -s /path/to/local/copy/of/reveal.js/ .
google-chrome http://127.0.0.1:8001/`
but now I have
127.0.0.1 - - [31/Mar/2015 12:07:29] code 404, message File not found
127.0.0.1 - - [31/Mar/2015 12:07:29] "GET /custom.css HTTP/1.1" 404 -
examining the source of my.slides.html I see the lines
<!-- Custom stylesheet, it must be in the same directory as the html file -->
<link rel="stylesheet" href="custom.css">
so I'm bound to the conclusion that --post serve does an awful lot of things at my back and that I'm out of luck in my attempt to save a standalone slide-show and have it served by a local HTTP server.
How can I have a properly served slide show without resorting to --post serve?

Booted Off Local Server - 302 error

I'll start with the log that I am receiving below:
Dec.15.11.56-Rf: Incoming Request URL: /
Dec.15.11.56-Rf: SECURE GET Path: / From: mlocal.cldeals.com Rewritten: www.cldeals.com
Dec.15.11.56-Rf: Received 302 Found [text/html; charset=UTF-8] response for /
Dec.15.11.56-Rf: Sending 302 text/html; charset=UTF-8 response for /
Dec.15.11.56-Rf: Stats. Total: 0.52088702, Upstream: 0.48212701, Processing: 0.00105600, ProcessingOther: 0.04037500
Basically, when I go to mlocal.cldeals.com, it loads fine. If I click on another page, say mlocal.cldeals.com/products, that loads fine as well. The issue seems to be when I go to the account page and try to switch back to the homepage, maybe some type of security issue? When I try to switch back to mlocal.cldeals.com, the home page, it boots me off and sends me to www.cldeals.com. Is there something I can add to force this from not happening? Additionally, is this just a local server issue that would go away when I launch it on Moovweb's server? Any help is greatly appreciated.
Thank you.
It looks like the backend response to https://www.cldeals.com is a 302 to http://www.cldeals.com:80/. Not sure why that is the case (see note below *)
curl -v -o /dev/null https://www.cldeals.com
This response contains a hardcoded Location header and your project is passing along the response as is, which is why you are being booted off your local server.
Because the Location header value has a port specified, you'll need to modify your config.json to include this line in the mapping:
{
"host_map": [
"$.cldeals.com => www.cldeals.com",
"$.cldeals.com => www.cldeals.com:80"
]
}
This way, the SDK knows to rewrite that specific host:port value... (By default all HTTP requests go through port 80, so that information isn't really necessary)
*This is might be bug in the backend implementation because once you log in, you should be in HTTPS mode until you log out. (I can see some pages with personal information being transmitted over plain HTTP)

bottle.run(app) returns 404 - Not Found

I'm trying to run this very simple script:
import bottle
app = bottle.Bottle()
#bottle.route('/test')
def test():
return 'hi'
bottle.run(app=app)
When I run the script, the bottle server starts correctly:
Bottle v0.11.6 server starting up (using WSGIRefServer())...
Listening on http://127.0.0.1:8080/
Hit Ctrl-C to quit.
and HTTP requests reach the server:
127.0.0.1 - - [10/Dec/2013 14:43:52] "GET /test HTTP/1.1" 404 728
Anyway I get a 404 response.
If I comment the third line and start bottle with bottle.run() everything works fine:
import bottle
#app = bottle.Bottle()
#bottle.route('/test')
def test():
return 'hi'
bottle.run() # RUN BOTTLE WITHOUT APP ARGUMENT
The HTTP response:
127.0.0.1 - - [10/Dec/2013 14:55:38] "GET /test HTTP/1.1" 200 2
I can't figure out what is the problem with the first snippet. Can you help me?
I found the error. I changed #bottle.route('/test') decorator with #app.route('/test').

Why do perl dancer pages render slowly (while I'm not connected to internet)?

I'm running Dancer and found it slow -- pages took a long time to render.
This is the example code from Dancer::Introduction:
#!/usr/bin/perl
# make this script a webapp
use Dancer;
# declare routes/actions
get '/' => sub {
"Hello World";
};
get '/hello/:name' => sub {
"Hello ".param('name');
};
# run the webserver
Dancer->dance;
It takes my browser 10 seconds to get&render the response( using firebug in firefox ).
And Dancer message:
[20734] core #0.000228> request: GET / from 192.168.1.101 in /usr/lib/perl5/site_perl/5.8.8/Dancer/Handler.pm l. 57
[20734] core #0.000809> [hit #44]trying to match `/' against /^\/$/ in /usr/lib/perl5/site_perl/5.8.8/Dancer/Route.pm l. 84
[20734] core #0.000953> [hit #44] --> got 1 in /usr/lib/perl5/site_perl/5.8.8/Dancer/Route.pm l. 101
[20734] core #0.001645> [hit #44]response: 200 in /usr/lib/perl5/site_perl/5.8.8/Dancer/Handler.pm l. 175
[20734] core #0.000135> request: GET /favicon.ico from 192.168.1.101 in /usr/lib/perl5/site_perl/5.8.8/Dancer/Handler.pm l. 57
[20734] core #0.000873> [hit #45]response: 200 in /usr/lib/perl5/site_perl/5.8.8/Dancer/Handler.pm l. 175
Why is Dancer so slow? Did I miss something?
Is the computer connected to internet? I got the same problem when testing from a computer not connected to internet; fixed it by deleting
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>;
from main.tt
As you can see from the debugging log, it took Dancer 0.6ms to serve the request. The problem is somewhere else in the stack. A frequent culprit is reverse DNS — the webserver tries to reverse-lookup the remote IP address for access logging purposes, and if your DNS is misconfigured, that can take quite a while (sometimes 30 or 60 seconds) before it fails.
Use Dancer::Plugin::NYTProf to profile your application. From the documentation:
By simply loading this plugin, you'll have the detailed, helpful
profiling provided by Devel::NYTProf.
Each individual request to your app is profiled. Going to the URL
/nytprof in your app will present a list of profiles.
Or, if it's a browser-side issue, you can use an extension like Firebug to see what part of a page load is slow.