Hi, all,
i think this is a bug about the constant defined in sinatra, let's look my code.
route.rb
require 'sinatra'
get '/' do
C = "this is a test for constant"
"Hello World!"
end
Gemfile
source 'http://rubygems.org'
gem 'rack'
gem 'sinatra'
config.ru
require './route'
run Sinatra::Application
Starting the web server, we will see the below
$ rackup
[2011-10-08 19:54:36] INFO WEBrick 1.3.1
[2011-10-08 19:54:36] INFO ruby 1.9.2 (2011-07-09) [i686-linux]
[2011-10-08 19:54:36] INFO WEBrick::HTTPServer#start: pid=3268 port=9292
127.0.0.1 - - [08/Oct/2011 19:54:42] "GET / HTTP/1.1" 200 25 0.0059
127.0.0.1 - - [08/Oct/2011 19:54:42] "GET / HTTP/1.1" 200 25 0.0142
/home/zcdny/repo/test/route.rb:4: warning: already initialized constant C
127.0.0.1 - - [08/Oct/2011 19:54:43] "GET / HTTP/1.1" 200 25 0.0094
127.0.0.1 - - [08/Oct/2011 19:54:43] "GET / HTTP/1.1" 200 25 0.0098
/home/zcdny/repo/test/route.rb:4: warning: already initialized constant C
127.0.0.1 - - [08/Oct/2011 19:54:55] "GET / HTTP/1.1" 200 25 0.0003
127.0.0.1 - - [08/Oct/2011 19:54:55] "GET / HTTP/1.1" 200 25 0.0006
/home/zcdny/repo/test/route.rb:4: warning: already initialized constant C
127.0.0.1 - - [08/Oct/2011 19:54:56] "GET / HTTP/1.1" 200 25 0.0003
127.0.0.1 - - [08/Oct/2011 19:54:56] "GET / HTTP/1.1" 200 25 0.0005
Eidt
Fixing the file route.rb
require 'sinatra'
configure do
C = "this is a test for constant"
end
get '/' do
"Hello World!"
end
Now, the server no longer warning the constant be initialized.
But the log of server still appears double 'GET' require ,
i just want it requires for one to every client required, that is my question, How to solve it.
Thanks in advance.
What's wrong about that? If you define the constant twice (which happens if you have two GET requests or a GET and a HEAD request) then that warning will be displayed. You global variables instead. But if you don't have to, try to avoid global state at all cost, otherwise you might run in architectural issues (what if you want to serve more endpoints and globals clash) and make it hard to scale: if you rely on the internal state of a process, will you be able to serve the website from two processes? what about two machines?
Related
I am testing REST API using rest-assured. When I send POST request it looks like rest-assured is making extra call. Here is the output from /var/log/httpd/access_log:
11.31.41.111 - - [26/Nov/2019:19:39:14 +0000] "POST /rest/v1/contact HTTP/1.1" 401 340 "-" "Apache-HttpClient/4.5.3 (Java/1.8.0_221)"
11.31.41.111 - - [26/Nov/2019:19:39:14 +0000] "POST /rest/v1/contact HTTP/1.1" 200 515 "-" "Apache-HttpClient/4.5.3 (Java/1.8.0_221)"
When I send exactly same request using Postman, access log shows only one request comes to the server:
11.31.41.111 - - [26/Nov/2019:19:40:44 +0000] "POST /rest/v1/contact/ HTTP/1.1" 200 529 "-" "PostmanRuntime/7.19.0"
Any idea why this is happening?
You should use Preemptive Authentication when building RestAssured request specification. Here's an example:
RestAssured.given().auth().preemptive().basic("username", "password")
.when().get("http://example.com")
.then().statusCode(200);
When using HAProxy, I've been getting the error 413: Request Entity Too Large
This error occurs when I'm trying to upload some files which are too large, however I can not find any documentation on how to increase this limit.
How can you increase the maximum upload limit to a specified amount of MB's?
This is not a HAProxy error, as you can see here http://cbonte.github.io/haproxy-dconv/configuration-1.7#1.3.1, 413 Error is not in the list.
So this probably an error returned from the server and HAProxy is just "forwarding" the error to the client.
To be 100% sure, you can see the logs:
An error returned by HAProxy:
127.0.0.1:35494 fe_main be_app/websrv1 0/0/-1/-1/3002 503 212 - - SC-- 0/0/0/0/3 0/0 "GET /test HTTP/1.1"
An error returned by the backend server:
127.0.0.1:39055 fe_main be_app/websrv2 0/0/0/0/0 404 324 - - --NI 1/1/0/1/0 0/0 "GET /test HTTP/1.1"
Notice the "-1" in the timers.
Using Eclipse's internal representation of Tomcat8 (do not know if that is relevant), the following is a series of page requests. This comes from the file:
C:\Users\BS\Eclipse\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\logs\localhost_access_log.2016-04-25.txt:
"GET /ProjK/ HTTP/1.1" 200 16354
"GET /ProjK/WEFiles/Css/v02/openElement.css?v=50491098000 HTTP/1.1" 200 16354
"GET /ProjK/Templates/BaseLayer.css?v=50491098000 HTTP/1.1" 200 16354
"GET /ProjK/index.css?v=50491098000 HTTP/1.1" 200 16354
"GET /ProjK/WEFiles/Client/jQuery/migrate.js?v=50491098000 HTTP/1.1" 200 16354
"GET /ProjK/WEFiles/Client/Common/oe.min.js?v=50491098000 HTTP/1.1" 200 16354
"GET /ProjK/Files/Image/Template/Top.png HTTP/1.1" 200 16354
"GET /ProjK/Files/Image/Template/Logo.png HTTP/1.1" 200 16354
"GET /ProjK/Files/Image/BioTransparent.png HTTP/1.1" 200 16354
"GET /ProjK/WEFiles/Image/WEImage/PuceImg-3f204a74.png HTTP/1.1" 200 16354
"GET /ProjK/WEFiles/Image/empty.png HTTP/1.1" 200 16354
Observe that each response is the same byte count. The browser receives the content of the same file regardless of the request.
The content is from "index.html" that has been renamed to "index.ftl" and renamed back to "index.html". I have been making simple edits to this file hoping to see any change in what the browser shows.
I see that the file "index.html" has been updated with the new content in both the workspace and the /tmp0/webapps/ folder.
Yet, there are no changes in what is delivered to the browser.
A number of conversations suggests that Tomcat caching be dealt with. I would have to wonder why, during app development, this is an actual consideration.
But caching does not explain why every page and page resource request is served the same unchanging content.
What common mistake would a new java web app developer be making with respect to the setup/configuration of Tomcat and/or Eclipse and or the webapp that would explain this behavior?
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').
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