What's Lua's equivalent to php's $_GET for a web application?
Also if the url is something like index.cgi?thisisatest how can I get everything after the question mark?
In the context of lighttpd and mod_magnet, query strings are not parsed automatically so you need to do it yourself. You can find an example here, look for "flv-streaming.lua" in the page.
As for your second question, Lorenzo gave you a generic answer, but in mod_magnet you can also use lighty.env["uri.query"] as seen in the same example.
Lua in itself is not a language for web development. There are some libraries for that. You can try luasocket.
As for your second question:
local url = "index.cgi?thisisatest"
local suffix = string.match( url, "^[^?]+?([^?]-)$" )
print( suffix )
If you are running your Lua code in Ophal, then you can use the functions: request_uri() and request_path()
If you're using lua as a CGI script, os.getenv("QUERY_STRING") will return everything after the question mark/
Related
May be a dumb question, but it's been bugging me recently. I see "URL=" inside alot of URL's, such as this one:
http://www.tierraantigua.com/search-2?url=http%3A%2F%2Flink.flexmls.com%2Fwws30ham
What exactly is this used to do? Is it part of the iFrame functionality? I know the last part of the URL (after the URL=) is the part being displayed in the iFrame, but I'm unsure of why it is included in the primary URL as well.
Thanks!
The url you see here is just a standard query parameter wit the name url and the encoded value http%3A%2F%2Flink.flexmls.com%2Fwws30ham which decodes to http://link.flexmls.com/Fwws30ham. Most of the times it is used for determining redirection or source information by the application you are using. It is entirely domain-specific and can have any meaning the website developer would like to use.
PHP GET
Description ΒΆ
An associative array of variables passed to the current script via the URL parameters.
$url = $_GET['url'];
echo $url; // http%3A%2F%2Flink.flexmls.com%2Fwws30ham
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Ruby code to extract host from URL string
I found this module called URI which can parse the url. (I'm pretty new to ruby. Is 'module' synonymous with 'library' in this case?) You can then extract the host name.
uri = URI.parse("http://www.ruby-lang.org/")
...
p uri.host
# => "www.ruby-lang.org"
From this, I suppose you could remove 'www.' and keep other subdomains using regular expressions.
Does anyone have a more straight-forward alternative or does this approach seem right?
So while posting my own answer, I'm not saying that gems like domainatrix or public_suffix_server aren't good elegant solutions (although the latter bugged on me immediately which caused me to go this route).
People suggesting using split() made me realize that I could just sub out 'www.' if it existed and otherwise leave domains as they are without installing gems and using 1 simple line of code:
url = request.original_url
domain = URI.parse(url).host.sub(/\Awww\./, '')
This works with subdomains and multi-part suffixes (eg. co.uk). Anybody see anything wrong with this?
EDIT: Thanks sorens for pointing out the weak regex I was originally using. This expression is certainly better.
You can use domainatrix gem to get what you want: url.domain + url.public_suffix, but you can just do some string manipulation like uri[4..-1].
I'm learning Lua at the moment. I need to be able to access post and get data. I'm trying to find out how the equivalent of PHP $_POST and $_GET in Lua.
This depends on the web server you are running in, and any intermediary libraries you are using.
In Apache 2.3, using the included mod_lua, it would be
function my_handler(r)
-- URI params
local simple, full = r:parseargs()
-- POST body
local simple, full = r:parsebody()
end
Where simple is a table of key -> value (what you want most of the time) and full is key -> [value1, value2, ...] for cases of duplicately named params.
Fuller examples are available at http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/lua/test/htdocs/test.lua?revision=728494&view=markup
There are many web-frameworks for Lua, each with its own way of accessing GET and POST.
Probably easiest way to learn Lua for the web-development is to use WSAPI.
To get GET and POST, use wsapi.request in your handler:
require 'wsapi.request'
local handler = function(env)
local request = wsapi.request.new(env)
local GET = wsapi.request.GET
local POST = wsapi.request.POST
...
end
There is no equivalent as Lua is not designed as a web scripting language. In what context are you using this (CGI, FCGI, Apache module)? You'll probably need to look into the CGI specification and accessing environment variables and stdin from Lua.
You could always check out Lua4Web https://github.com/schme16/Lua4Web
Reading POST data in traditional html-form or url-encoded format is a mess in Lua. Better you try to use AJAX forms javascript library, so you get your data sent in JSON back to the server where you can easily parse and use.
Is there an equivalent to PHP's urlencode in Ruby on Rails 2.3.5? (It encodes a string to be used in a query part of a URL)
I googled it but all the answers seem to date back to before 2006 and seems dates.
This is what I found. It seems a bit abnormal to call CGI::escape in a view.
Is there an equivalent helper function?
Thanks!
I believe the u helper method is what you're looking for:
<%=u "URL ENCODE <p>ME</p>" %>
This uses the method ERB::Util.url_encode, which is aliased to u.
You can find the documentation for this method here: http://rdoc.info/stdlib/erb/1.8.7/ERB/Util:url_encode.
If you want to do it without ERB, you can use the following:
Rack::Utils.escape('http://example.com')
#=> "http%3A%2F%2Fexample.com"
Which will also convert /
This worked better for me than the Rack::Utils.escape:
URI::escape('http://example.com/?param=Hello World')
Because it replaced the spaces with %20 instead of +
But it won't replace /
ERB::Util.html_escape, which is aliased to h and ERB::Util.url_encode, which is aliased to u .
http://ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB/Util.html
The method names seem to have changed since Sam Soffes answer, but the aliases haven't.
i have written some code in php there i have use mhash(MHASH_SHA256, $key) and its giving result as expected.i wanna know how we can achieve same thing in erlang.i can see in crypto their is one inbuild sha function is their but i dont think so its mean for sha256.
any suggestion what i have to do ?
thank you in advance.
Have you seen this page, which links to an SHA-256 module for Erlang?
EDIT: Apparently that code is obsolete, replaced by this module. If that still doesn't do what you want (in terms of hex/binary) I suggest you email its author, preferably with a patch.
It seems to me that the return value of the mentioned sha2 module depends on your input. If you call it with a binary, the result is binary; if you call it with a string, the result is a string:
sha2:hexdigest256("Zed").
"a90e4dc685583c72296ca49b5d0bb148f2e1197a805b2a1d2ff6d17b4398b2be"
sha2:hexdigest256(<<"Zed">>).
<<169,14,77,198,133,88,60,114,41,108,164,155,93,11,177,72,
242,225,25,122,128,91,42,29,47,246,209,123,67,...>>