Getting 500 response with Erlang Cowboy app - erlang

I'm having trouble deciphering this error message that I'm getting when hitting a simple endpoint created with Cowboy. I created a simple app with cowboy (https://github.com/overture8/cow_app) then started the app using rebar3 shell (not sure if this is correct?). Anyway, I'm getting this error when hitting the endpoint:
Error in process <0.232.0> with exit value:
{[{reason,undef},
{mfa,{hello_handler,init,3}},
{stacktrace,
[{hello_handler,init,
[{tcp,http},
{http_req,#Port<0.7138>,ranch_tcp,keepalive,<0.232.0>,<<"GET">>,
'HTTP/1.1',
{{127,0,0,1},49651},
<<"127.0.0.1">>,undefined,8010,<<"/">>,undefined,<<>>,
undefined,[],
[{<<"host">>,<<"127.0.0.1:8010">>},
{<<"connection">>,<<"keep-alive">>},
{<<"cache-control">>,<<"max-age=0">>},
{<<"upgrade-insecure-requests">>,<<"1">>},
{<<"user-agent">>,
<<"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36">>},
{<<"accept">>,
<<"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8">>},
{<<"dnt">>,<<"1">>},
{<<"accept-encoding">>,<<"gzip, deflate, sdch">>},
{<<"accept-language">>,
<<"en-GB,en;q=0.8,en-US;q=0.6,fr;q=0.4">>}],
[{<<"connection">>,[<<"keep-alive">>]}],
undefined,[],waiting,<<>>,undefined,false,waiting,[],<<>>,
undefined},
[]],
[]},
.
.
.
Maybe I'm just doing something completely wrong - this is my first experience with using Erlang.
Any help would be greatly appreciated.

Your rebar.lock is out of sync with rebar.config and is pointing to version 1.0.1 of cowboy which requires init/3 to be exported, not init/2, which is what the error ... {reason,undef}, {mfa,{hello_handler,init,3}}, ... means.
To fix, run rebar3 upgrade cowboy and then run rebar3 shell. After I run that, the application works fine for me:
$ curl -i http://localhost:8010/
HTTP/1.1 200 OK
server: Cowboy
date: Wed, 07 Sep 2016 09:57:22 GMT
content-length: 13
content-type: text/plain
Hello Erlang!

Related

erlang gen_tcp connecting to erlang.org claims a 404

context: JA's "Programming Erlang" 2ed, chapter 16 on files, page 256, example on working with parsing urls from a Binary.
The steps suggested (after writing code for the scavenge_urls module) are these:
B = socket_examples:nano_get_url("www.erlang.org"),
L = scavenge_urls:bin2urls(B),
scavenge_urls:urls2htmlFile(L,"gathered.html").
And that fails (subtly) - the list L ends up being empty. Running the first step on its own, a strange thing is observed - it does return a binary, but it's not the binary I was looking for:
9> B.
<<"HTTP/1.1 404 Not Found\r\nServer: nginx\r\nDate: Sun, 19 Nov 2017 01:57:07 GMT\r\nContent-Type: text/html; charset=UTF-8\r\n"...>>
shows that this is where the problem lies.
yet in the browser all's good with the mothership! I was able to complete the exercise by replacing the call to socket_examples:nano_get_urls/1 with, first, CURLing for the same url, dumping that into a file, and then file:read_file/1. The next steps all ran fine.
Peeking inside the socket_examples module, I see this:
nano_get_url(Host) ->
{ok,Socket} = gen_tcp:connect(Host,80,[binary, {packet, 0}]), %% (1)
ok = gen_tcp:send(Socket, "GET / HTTP/1.0\r\n\r\n"), %% (2)
receive_data(Socket, []).
receive_data(Socket, SoFar) ->
receive
{tcp,Socket,Bin} -> %% (3)
receive_data(Socket, [Bin|SoFar]);
{tcp_closed,Socket} -> %% (4)
list_to_binary(reverse(SoFar)) %% (5)
end.
Nothing looks suspicious. First it establishes the connection, next it fires a GET, and then it receives the response. I've never before had to explicitly connect first, and fire a GET second, my http client libraries hid that from me. So maybe I don't know what to look for... and I sure trust Joe's code doesn't have any glaring mistakes! =) Yet the lines with comments (3),(4) and (5) aren't something I fully understand.
So, any ideas, fellow Erlangers?
Thank a bunch!
The problem is not Erlang. It looks like the server running erlang.org requires a Host header as well:
$ nc www.erlang.org 80
GET / HTTP/1.0
HTTP/1.1 404 Not Found
Server: nginx
Date: Sun, 19 Nov 2017 05:51:39 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 162
Connection: close
Vary: Accept-Encoding
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>
$ nc www.erlang.org 80
GET / HTTP/1.0
Host: www.erlang.org
HTTP/1.1 200 OK
Server: nginx
Date: Sun, 19 Nov 2017 05:51:50 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 12728
Connection: close
Vary: Accept-Encoding
<!DOCTYPE html>
<html>
...
Your Erlang code also works with the Host header after GET HTTP/1.0\r\n:
1> Host = "www.erlang.org".
"www.erlang.org"
2> {ok, Socket} = gen_tcp:connect(Host, 80, [binary, {packet, 0}]).
{ok,#Port<0.469>}
3> ok = gen_tcp:send(Socket, "GET / HTTP/1.0\r\nHost: www.erlang.org\r\n\r\n").
ok
4> flush().
Shell got {tcp,#Port<0.469>,
<<"HTTP/1.1 200 OK\r\nServer: nginx\r\n...>>
Shell got {tcp_closed,#Port<0.469>}

Cannot log in Neo4j server

On a fresh install of neo4j-community-2.2.4 I get the message "Invalid username or password." when submitting the login form at localhost:7474/browser with the default username neo4j and password neo4j.
I did enable org.neo4j.server.webserver.address=0.0.0.0 and dbms.security.auth_enabled=true with a server stop and start and a browser shift-reload.
I then changed the property org.neo4j.server.webserver.address=127.0.0.1 to match my /etc/hosts and tried on 127.0.0.1:7474/browser but got the same message.
Here is the browser console output:
Remote Address:127.0.0.1:7474
Request URL:http://localhost:7474/db/data/
Request Method:GET
Status Code:401 Unauthorized
Request Headersview source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,fr;q=0.6,ru;q=0.4,es;q=0.2,sv;q=0.2,nb;q=0.2,et;q=0.2
Authorization:Basic bmVvNGo6bmVvNGo=
Cache-Control:no-cache
Connection:keep-alive
Cookie:languageCodeAdmin=en; PHPSESSID=vcbvfkvj3shajhlh5u2pue9i70; admin_template_phone_client=0; admin_template_touch_client=0; admin_template_model=1
Host:localhost:7474
If-Modified-Since:Wed, 11 Dec 2013 08:00:00 GMT
Pragma:no-cache
Referer:http://localhost:7474/browser/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/37.0.2062.120 Chrome/37.0.2062.120 Safari/537.36
X-stream:true
Response Headersview source
Content-Length:135
Content-Type:application/json; charset=UTF-8
Date:Sat, 19 Sep 2015 09:14:03 GMT
Server:Jetty(9.2.4.v20141103)
WWW-Authenticate:None
{
"errors" : [ {
"message" : "Invalid username or password.",
"code" : "Neo.ClientError.Security.AuthorizationFailed"
} ]
}
So I then tried to change the default password.
To do that, I first disabled the authentication with a dbms.security.auth_enabled=false and a server stop and start.
And I then tried the following curl request:
curl -H "Accept:application/json; charset=UTF-8" -H "Content-Type: application/json" "http://localhost:7474/user/neo4j/password" -X POST -d "{ \"password\" : \"neo4j\" }" -i
But it gives me the response:
HTTP/1.1 404 Not Found
Date: Sat, 19 Sep 2015 09:25:38 GMT
Access-Control-Allow-Origin: *
Content-Length: 0
Server: Jetty(9.2.4.v20141103)
Since you get an authentication error on your browser request which uses the default credentials neo4j:neo4j your database has already a different password configured. To reset it to default, stop neo4j, delete data/dbms/auth and start it again. Now you can use the default pw.
In my case Neo4J windows services was not installed just followed below steps
neo4j.bat install-service
neo4j.bat status
neo4j.bat start
it worked, cheers

Cannot get access_token from oauth2-example-orion-client

I'm using oauth2-example-orion-client from https://github.com/fgalan/oauth2-example-orion-client/ , and I followed the steps, however after log in with my filab account (I´ve already created an application to get the Oauth credentials), I get the following error when it the to go to 'callback_uri'/login:
express deprecated app.configure: Check app.get('env') in an if statement server.js:16:5
Server listen in port 80. Connect to localhost
127.0.0.2 - - [Tue, 05 Aug 2014 20:43:22 GMT] "GET / HTTP/1.1" 302 342 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131029 Firefox/17.0"
/home/nmerino/Desktop/oauth2sample/server.js:85
req.session.oauth_token = results.access_token;
^
TypeError: Cannot read property 'access_token' of undefined
at /home/nmerino/Desktop/oauth2sample/server.js:85:42
at /home/nmerino/Desktop/oauth2sample/oauth2.js:138:18
at ClientRequest.<anonymous> (/home/nmerino/Desktop/oauth2sample/oauth2.js:115:5)
at ClientRequest.EventEmitter.emit (events.js:95:17)
at CleartextStream.socketErrorListener (http.js:1487:9)
at CleartextStream.EventEmitter.emit (events.js:95:17)
at SecurePair.<anonymous> (tls.js:1302:19)
at SecurePair.EventEmitter.emit (events.js:92:17)
at SecurePair.maybeInitFinished (tls.js:896:10)
at CleartextStream.read [as _read] (tls.js:430:15)
Althought the message make seem I´m using localhost, I´m using 127.0.0.2 instead, to run the oauth.
Please I need help with these errors.
Thanks
You can get the access token using the token_script.sh script, as described in the Orion quick start guide.

RSS parsing error on Heroku

I'm doing some simple RSS parsing and rendering - works perfectly fine on localhost and production at Heroku, but for some reason this particular RSS feed works only on localhost, but not when deployed at Heroku.
Here is the RSS feed:
http://careers.joelonsoftware.com/jobs/feed?tags=network
Here is the error I get on Heroku:
A ActionView::Template::Error occurred in jobs#index:
503 Service Unavailable
/usr/local/lib/ruby/1.9.1/open-uri.rb:346:in `open_http'
Here is my code:
#rss = RSS::Parser.parse(the_rss_feed, false)
render :partial => "layouts/rss_syndicated_jobs", :locals => {:rss => #rss}
I'm requiring:
require 'rss/2.0'
require 'open-uri'
This seems to be a perfectly valid RSS feed - and again, works fine on localhost - but not when deployed at Heroku....any thoughts?
That domain doesn't like heroku's servers making HTTP GET requests.
From Heroku
➜ deefour ✗ heroku run "curl -s -D - careers.joelonsoftware.com/jobs/feed -o /dev/null"
Running `curl -s -D - careers.joelonsoftware.com/jobs/feed -o /dev/null` attached to terminal... up, run.9211
HTTP/1.0 503 Service Unavailable
Cache-Control: no-cache
Connection: close
Content-Type: text/html
From Local Machine
➜ deefour ✗ curl -s -D - careers.joelonsoftware.com/jobs/feed -o /dev/null
HTTP/1.1 200 OK
Cache-Control: private, max-age=1800
Content-Type: application/rss+xml; charset=utf-8
Expires: Tue, 27 Nov 2012 02:46:38 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Headers: x-requested-with
Access-Control-Max-Age: 60
Date: Tue, 27 Nov 2012 02:16:38 GMT
Content-Length: 10982
If you print the response body through Heroku it provides the following info
This IP is only allowed to access our API.
To protect our users, we can't process requests from this IP address.
If you believe you have reached this page in error, contact us.

RavenDb - The remote server returned an error: (403) Forbidden

When I try to create a database people collection on RavenDb, I get the following error:
The remote server returned an error: (403) Forbidden.
I hots the Raven on IIS and I am not sure what is going on. On the raven Management Studio, when I try to create a database, I get the below result:
Could not authenticate against server
Message: The remote server returned an error: NotFound. Uri:
/databases?database=Default Server Uri:
http://localhost:8888/docs/Raven/Databases/People
-- Error Information -- System.Net.WebException: The remote server returned an error: NotFound. at
System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult
asyncResult) at System.Func2.Invoke(T arg) at
System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult
iar, Func2 endMethod, TaskCompletionSource1 tcs)
When I looked into this with process monitor, I see that I am getting bunch of not found errors:
Under C:\Utils\RavenDB\Web directory, there are Bin and Data folders, nothing more. Should I create the necessary folders by myself? I created the docs folder but nothing changed. Still having problems.
Any thoughts?
Edit:
Tried with fiddler on Raven Management Studio by creating a document:
Request:
PUT http://localhost:1786/docs/People HTTP/1.1
Accept: /
Accept-Language: en-US
Referer: http://localhost:1786/silverlight/Raven.Studio.xap
Content-Length: 24
Accept-Encoding: identity
If-None-Match: 00000000-0000-0000-0000-000000000000
Content-Type: application/json; charset=utf-8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7
(KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7
Host: localhost:1786
Connection: Keep-Alive
Pragma: no-cache
{
"Name": "..."
}
Response:
HTTP/1.1 403 Forbidden
Cache-Control: private
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Thu, 08 Mar 2012 12:33:22 GMT
Content-Length: 0
This was answered in the comments, however I'll post it here for posterity.
The issue is resolved by changing the web.config value:
<add key="Raven/AnonymousAccess" value="Get"/>
To:
<add key="Raven/AnonymousAccess" value="All"/>
Here is the link to the config page that explains each of the options. Be aware that this option will in fact turn off authentication, if you wish to run with authentication you will need the 'Get' or 'None' setting and you will also need to make sure Windows Authentication is installed (IIS 7.5) and enabled on the Raven IIS Application.

Resources