Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Patch for fdsrv support
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Roberto Saccon  
View profile  
 More options Nov 24 2007, 9:11 pm
From: Roberto Saccon <rsac...@gmail.com>
Date: Sat, 24 Nov 2007 18:11:57 -0800 (PST)
Local: Sat, Nov 24 2007 9:11 pm
Subject: Patch for fdsrv support
I'd like to use MochiWeb also for ports below 1024 without reverse-
proxying. Unfortunately I haven't found a way to extend Mochiweb with
the fdsrv stuff without hacking mochiweb_socket_server.erl. If there
exists a way to do so, please let me know, otherwise the patch below
might be useful for others and of course I would love to see it
applied to trunk !

regards
Roberto

Index: /Users/rsaccon/opensource/mochiweb-read-only/src/
mochiweb_socket_server.erl
===================================================================
--- /Users/rsaccon/opensource/mochiweb-read-only/src/
mochiweb_socket_server.erl      (revision 20)
+++ /Users/rsaccon/opensource/mochiweb-read-only/src/
mochiweb_socket_server.erl      (working copy)
@@ -21,7 +21,8 @@
         max=2048,
         ip=any,
         listen=null,
-        acceptor=null}).
+        acceptor=null,
+     fdsrv=false}).

 start(State=#mochiweb_socket_server{}) ->
     start_server(State);
@@ -63,6 +64,8 @@
     parse_options(Rest, State#mochiweb_socket_server{port=Port});
 parse_options([{port, Port} | Rest], State) ->
     parse_options(Rest, State#mochiweb_socket_server{port=Port});
+parse_options([{fdsrv, Fdsrv} | Rest], State) ->
+    parse_options(Rest, State#mochiweb_socket_server{fdsrv=Fdsrv});
 parse_options([{ip, Ip} | Rest], State) ->
     ParsedIp = case Ip of
                   any ->
@@ -93,7 +96,7 @@
            gen_server:start_link(Name, ?MODULE, State, [])
     end.

-init(State=#mochiweb_socket_server{ip=Ip, port=Port}) ->
+init(State=#mochiweb_socket_server{ip=Ip, port=Port, fdsrv=Fdrsv}) ->
     process_flag(trap_exit, true),
     BaseOpts = [binary,
                {reuseaddr, true},
@@ -107,13 +110,31 @@
               Ip ->
                   [{ip, Ip} | BaseOpts]
           end,
+    case Fdrsv of
+        true ->
+            case fdsrv:start() of
+                {ok, _} ->
+                    case fdsrv:bind_socket(tcp, Port) of
+                        {ok, Fd} ->
+                            gen_tcp_listen(Port, [{fd, Fd} | Opts],
State);
+                        _ ->
+                            {stop, fdsrv_bind_failed}
+                    end;
+                _ ->
+                    {stop, fdsrv_start_failed}
+            end;
+        _ ->
+            gen_tcp_listen(Port, Opts, State)
+    end.
+
+gen_tcp_listen(Port, Opts, State) ->
     case gen_tcp:listen(Port, Opts) of
-       {ok, Listen} ->
-           {ok, ListenPort} = inet:port(Listen),
-           {ok, new_acceptor(State#mochiweb_socket_server{listen=Listen,
-                                                          port=ListenPort})};
-       {error, Reason} ->
-           {stop, Reason}
+        {ok, Listen} ->
+           {ok, ListenPort} = inet:port(Listen),
+                   {ok,
new_acceptor(State#mochiweb_socket_server{listen=Listen,
+
port=ListenPort})};
+       {error, Reason} ->
+                   {stop, Reason}
     end.

 new_acceptor(State=#mochiweb_socket_server{max=0}) ->
@@ -163,9 +184,15 @@
 handle_cast(stop, State) ->
     {stop, normal, State}.

-terminate(_Reason, #mochiweb_socket_server{listen=Listen}) ->
+terminate(_Reason, #mochiweb_socket_server{listen=Listen,
fdsrv=Fdsrv}) ->
     gen_tcp:close(Listen),
-    ok.
+    case Fdsrv of
+        true ->
+            fdsrv:stop(),
+            ok;
+        _ ->
+            ok
+    end.

 code_change(_OldVsn, State, _Extra) ->
     State.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bob Ippolito  
View profile  
 More options Nov 25 2007, 6:55 am
From: "Bob Ippolito" <b...@redivi.com>
Date: Sun, 25 Nov 2007 03:55:59 -0800
Local: Sun, Nov 25 2007 6:55 am
Subject: Re: [mochiweb] Patch for fdsrv support
It'd probably be better to just assume that if port =< 1024 then it
should try fdsrv, which wouldn't change the record layout. The only
times you'd be allowed to open those ports without fdsrv are windows
(maybe?) and running as root and neither of those should be done (in
production at least).

I'm not really very convinced that fdsrv is a great idea. Using a
reverse proxy or pf/iptables seems like a much better solution to me.
We need something like nginx anyway to serve static content, load
balance, and to send some URLs to other services (e.g. php over
fastcgi, python http servers, etc.). Why is fdsrv important to you?

-bob

On 11/24/07, Roberto Saccon <rsac...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roberto Saccon  
View profile  
 More options Nov 25 2007, 10:24 am
From: Roberto Saccon <rsac...@gmail.com>
Date: Sun, 25 Nov 2007 07:24:30 -0800 (PST)
Local: Sun, Nov 25 2007 10:24 am
Subject: Re: Patch for fdsrv support

On Nov 25, 9:55 am, "Bob Ippolito" <b...@redivi.com> wrote:

> It'd probably be better to just assume that if port =< 1024 then it
> should try fdsrv, which wouldn't change the record layout. The only
> times you'd be allowed to open those ports without fdsrv are windows
> (maybe?) and running as root and neither of those should be done (in
> production at least).

that makes perfect sense. At my app I actually set the fsdrv property
based on that criteria. The only reason I suggested to modify the
record, is hat I heard that there exist patches for running Linux and
FreeBSD with port < 1024 as non-root.

> I'm not really very convinced that fdsrv is a great idea. Using a
> reverse proxy or pf/iptables seems like a much better solution to me.
> We need something like nginx anyway to serve static content, load
> balance, and to send some URLs to other services (e.g. php over
> fastcgi, python http servers, etc.). Why is fdsrv important to you?

For two reasons, and I really hope I am wrong on both ! First, because
it is so easy to set up. Second, the main reason, I do chunked
responses, were the HTPP connection stays open (for erlycomet, and
also for RTMPT flash video) and long time ago when I was messing with
yaws, I investigated about nginx and I was told at that time that
reverse proxying was not suited for streaming type of applications. I
would love to hear that this has changed in the meantime !

regards
Roberto


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bob Ippolito  
View profile  
 More options Nov 25 2007, 3:10 pm
From: "Bob Ippolito" <b...@redivi.com>
Date: Sun, 25 Nov 2007 12:10:27 -0800
Local: Sun, Nov 25 2007 3:10 pm
Subject: Re: [mochiweb] Re: Patch for fdsrv support
On 11/25/07, Roberto Saccon <rsac...@gmail.com> wrote:

> On Nov 25, 9:55 am, "Bob Ippolito" <b...@redivi.com> wrote:
> > It'd probably be better to just assume that if port =< 1024 then it
> > should try fdsrv, which wouldn't change the record layout. The only
> > times you'd be allowed to open those ports without fdsrv are windows
> > (maybe?) and running as root and neither of those should be done (in
> > production at least).

> that makes perfect sense. At my app I actually set the fsdrv property
> based on that criteria. The only reason I suggested to modify the
> record, is hat I heard that there exist patches for running Linux and
> FreeBSD with port < 1024 as non-root.

Well if someone needs to do that sans fdsrv then I'm sure they'll
speak up. Maybe the patch could try gen_tcp first, and if it fails,
then try fdsrv (or vice versa)? On closing the socket you could just
close it both ways and catch the badarg, there's no meaningful result
or error code from closing a socket anyway.

> > I'm not really very convinced that fdsrv is a great idea. Using a
> > reverse proxy or pf/iptables seems like a much better solution to me.
> > We need something like nginx anyway to serve static content, load
> > balance, and to send some URLs to other services (e.g. php over
> > fastcgi, python http servers, etc.). Why is fdsrv important to you?

> For two reasons, and I really hope I am wrong on both ! First, because
> it is so easy to set up. Second, the main reason, I do chunked
> responses, were the HTPP connection stays open (for erlycomet, and
> also for RTMPT flash video) and long time ago when I was messing with
> yaws, I investigated about nginx and I was told at that time that
> reverse proxying was not suited for streaming type of applications. I
> would love to hear that this has changed in the meantime !

Okay, that sounds reasonable. If you put together a new patch that
uses fdsrv for Port < 1024 instead of changing the record then I'll
apply it.

-bob


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roberto Saccon  
View profile  
 More options Nov 25 2007, 9:06 pm
From: Roberto Saccon <rsac...@gmail.com>
Date: Sun, 25 Nov 2007 18:06:24 -0800 (PST)
Local: Sun, Nov 25 2007 9:06 pm
Subject: Re: Patch for fdsrv support
Here is the updated patch with fdsrv for Port < 1024 and not touching
the reocrd:

Index: /Users/rsaccon/opensource/mochiweb-read-only/src/
mochiweb_socket_server.erl
===================================================================
--- /Users/rsaccon/opensource/mochiweb-read-only/src/
mochiweb_socket_server.erl      (revision 20)
+++ /Users/rsaccon/opensource/mochiweb-read-only/src/
mochiweb_socket_server.erl      (working copy)
@@ -107,13 +107,36 @@
               Ip ->
                   [{ip, Ip} | BaseOpts]
           end,
+    case gen_tcp_listen(Port, Opts, State) of
+        {error, Reason} ->
+            if
+                Port < 1024 ->
+                    case fdsrv:start() of
+                        {ok, _} ->
+                            case fdsrv:bind_socket(tcp, Port) of
+                                {ok, Fd} ->
+                                    gen_tcp_listen(Port, [{fd, Fd} |
Opts], State);
+                                _ ->
+                                    {stop, fdsrv_bind_failed}
+                            end;
+                        _ ->
+                            {stop, fdsrv_start_failed}
+                    end;
+                true ->
+                    {error, Reason}
+            end;
+        Ok ->
+            Ok
+    end.
+
+gen_tcp_listen(Port, Opts, State) ->
     case gen_tcp:listen(Port, Opts) of
-       {ok, Listen} ->
-           {ok, ListenPort} = inet:port(Listen),
-           {ok, new_acceptor(State#mochiweb_socket_server{listen=Listen,
-                                                          port=ListenPort})};
-       {error, Reason} ->
-           {stop, Reason}
+        {ok, Listen} ->
+           {ok, ListenPort} = inet:port(Listen),
+                   {ok,
new_acceptor(State#mochiweb_socket_server{listen=Listen,
+
port=ListenPort})};
+       {error, Reason} ->
+                   {stop, Reason}
     end.

 new_acceptor(State=#mochiweb_socket_server{max=0}) ->
@@ -163,9 +186,15 @@
 handle_cast(stop, State) ->
     {stop, normal, State}.

-terminate(_Reason, #mochiweb_socket_server{listen=Listen}) ->
+terminate(_Reason, #mochiweb_socket_server{listen=Listen, port=Port})
->
     gen_tcp:close(Listen),
-    ok.
+    if
+        Port < 1024 ->
+            fdsrv:stop(),
+            ok;
+        true ->
+            ok
+    end.

 code_change(_OldVsn, State, _Extra) ->
     State.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bob Ippolito  
View profile  
 More options Nov 26 2007, 7:46 pm
From: "Bob Ippolito" <b...@redivi.com>
Date: Mon, 26 Nov 2007 16:46:13 -0800
Local: Mon, Nov 26 2007 7:46 pm
Subject: Re: [mochiweb] Re: Patch for fdsrv support
Can you send this as an attachment? Gmail and/or google groups mangled
the patch.

On 11/25/07, Roberto Saccon <rsac...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roberto Saccon  
View profile  
 More options Nov 26 2007, 8:36 pm
From: Roberto Saccon <rsac...@gmail.com>
Date: Mon, 26 Nov 2007 17:36:42 -0800 (PST)
Local: Mon, Nov 26 2007 8:36 pm
Subject: Re: Patch for fdsrv support
I uploaded mochiweb_socket_server.diff to the file section of the
google groups web interface

regards
Roberto

On Nov 26, 10:46 pm, "Bob Ippolito" <b...@redivi.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bob Ippolito  
View profile  
 More options Nov 27 2007, 12:14 am
From: "Bob Ippolito" <b...@redivi.com>
Date: Mon, 26 Nov 2007 21:14:30 -0800
Local: Tues, Nov 27 2007 12:14 am
Subject: Re: [mochiweb] Re: Patch for fdsrv support
Awesome, applied cleanly in r25. As a matter of style I changed the
"if" expressions to "case" (we don't otherwise use "if" anywhere), but
otherwise it's unmodified. Let me know if it still works, we don't
have fdsrv installed anywhere right now.

On 11/26/07, Roberto Saccon <rsac...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roberto Saccon  
View profile  
 More options Nov 27 2007, 5:58 am
From: Roberto Saccon <rsac...@gmail.com>
Date: Tue, 27 Nov 2007 02:58:23 -0800 (PST)
Local: Tues, Nov 27 2007 5:58 am
Subject: Re: Patch for fdsrv support
I tested it and discovered a problem:

we need to check for {stop, eacces} and not for {error, Reason} before
doing the fdsrv stuff

I have prepared a new patch "mochiweb_socket_server-2.diff" in the
filesection

regards
Roberto

On Nov 27, 3:14 am, "Bob Ippolito" <b...@redivi.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bob Ippolito  
View profile  
 More options Nov 27 2007, 9:58 am
From: "Bob Ippolito" <b...@redivi.com>
Date: Tue, 27 Nov 2007 06:58:37 -0800
Local: Tues, Nov 27 2007 9:58 am
Subject: Re: [mochiweb] Re: Patch for fdsrv support
Applied in r26.

On 11/27/07, Roberto Saccon <rsac...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roberto Saccon  
View profile  
 More options Nov 27 2007, 11:57 am
From: Roberto Saccon <rsac...@gmail.com>
Date: Tue, 27 Nov 2007 08:57:25 -0800 (PST)
Local: Tues, Nov 27 2007 11:57 am
Subject: Re: Patch for fdsrv support
great, thanks very much

On Nov 27, 12:58 pm, "Bob Ippolito" <b...@redivi.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »