summaryrefslogblamecommitdiffstats
path: root/archives/extend/2012-November.txt
blob: dedac05fe41cbe27c7b28a3274ed3a8b1d8926ba (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418

































































































































































































































































































































































































































                                                                                 
From essen at ninenines.eu  Tue Nov 13 10:04:17 2012
From: essen at ninenines.eu (=?ISO-8859-1?Q?Lo=EFc_Hoguin?=)
Date: Tue, 13 Nov 2012 10:04:17 +0100
Subject: [99s-extend] Proposal for Cowboy Routing
Message-ID: <[email protected]>

Hello!

I have put my thoughts on routing at the following gist:

   https://gist.github.com/4064759

Please comment or cry in terror if you're against it!

Thanks.

-- 
Lo?c Hoguin
Erlang Cowboy
Nine Nines
http://ninenines.eu


From thomas at oinksoft.com  Tue Nov 13 15:12:59 2012
From: thomas at oinksoft.com (Thomas Allen)
Date: Tue, 13 Nov 2012 09:12:59 -0500
Subject: [99s-extend] Proposal for Cowboy Routing
In-Reply-To: <[email protected]>
References: <[email protected]>
Message-ID: <[email protected]>

On Tue, Nov 13, 2012 at 10:04:17AM +0100, Lo?c Hoguin wrote:
> Hello!
> 
> I have put my thoughts on routing at the following gist:
> 
>   https://gist.github.com/4064759
> 
> Please comment or cry in terror if you're against it!

I think that this looks very good. The current router handles all of my
use cases, and this bit of sugar will definitely help to speed up
development.

As an aside, have you considered adding optional slash-add
functionality, so that /a/b will match "/a/b/"? This is pretty easy to
set up at the server level, but the front-line server doesn't know about
our dispatch rules and so it doesn't know if there is a matching
dispatch rule for the slash-added version of a given URL.

Thomas


From essen at ninenines.eu  Tue Nov 13 15:14:03 2012
From: essen at ninenines.eu (=?ISO-8859-1?Q?Lo=EFc_Hoguin?=)
Date: Tue, 13 Nov 2012 15:14:03 +0100
Subject: [99s-extend] Proposal for Cowboy Routing
In-Reply-To: <[email protected]>
References: <[email protected]>
 <[email protected]>
Message-ID: <[email protected]>

On 11/13/2012 03:12 PM, Thomas Allen wrote:
> On Tue, Nov 13, 2012 at 10:04:17AM +0100, Lo?c Hoguin wrote:
>> Hello!
>>
>> I have put my thoughts on routing at the following gist:
>>
>>    https://gist.github.com/4064759
>>
>> Please comment or cry in terror if you're against it!
>
> I think that this looks very good. The current router handles all of my
> use cases, and this bit of sugar will definitely help to speed up
> development.
>
> As an aside, have you considered adding optional slash-add
> functionality, so that /a/b will match "/a/b/"? This is pretty easy to
> set up at the server level, but the front-line server doesn't know about
> our dispatch rules and so it doesn't know if there is a matching
> dispatch rule for the slash-added version of a given URL.

/a/b already matches /a/b/.

-- 
Lo?c Hoguin
Erlang Cowboy
Nine Nines
http://ninenines.eu


From thomas at oinksoft.com  Tue Nov 13 15:35:29 2012
From: thomas at oinksoft.com (Thomas Allen)
Date: Tue, 13 Nov 2012 09:35:29 -0500
Subject: [99s-extend] Proposal for Cowboy Routing
In-Reply-To: <[email protected]>
References: <[email protected]>
 <[email protected]>
 <[email protected]>
Message-ID: <[email protected]>

On Tue, Nov 13, 2012 at 03:14:03PM +0100, Lo?c Hoguin wrote:
> /a/b already matches /a/b/.

Sorry for not being more precise: /a/b would result in a 301 redirect to
/a/b/

Thomas


From essen at ninenines.eu  Tue Nov 13 15:41:35 2012
From: essen at ninenines.eu (=?ISO-8859-1?Q?Lo=EFc_Hoguin?=)
Date: Tue, 13 Nov 2012 15:41:35 +0100
Subject: [99s-extend] Proposal for Cowboy Routing
In-Reply-To: <[email protected]>
References: <[email protected]>
 <[email protected]> <[email protected]>
 <[email protected]>
Message-ID: <[email protected]>

On 11/13/2012 03:35 PM, Thomas Allen wrote:
> On Tue, Nov 13, 2012 at 03:14:03PM +0100, Lo?c Hoguin wrote:
>> /a/b already matches /a/b/.
>
> Sorry for not being more precise: /a/b would result in a 301 redirect to
> /a/b/

Oh alright. Well they are equivalent URLs, so if you really need to 
redirect I would do so in the handler directly, or through a request hook.

-- 
Lo?c Hoguin
Erlang Cowboy
Nine Nines
http://ninenines.eu


From thomas at oinksoft.com  Tue Nov 13 16:12:14 2012
From: thomas at oinksoft.com (Thomas Allen)
Date: Tue, 13 Nov 2012 10:12:14 -0500
Subject: [99s-extend] Proposal for Cowboy Routing
In-Reply-To: <[email protected]>
References: <[email protected]>
 <[email protected]>
 <[email protected]>
 <[email protected]>
 <[email protected]>
Message-ID: <[email protected]>

On Tue, Nov 13, 2012 at 03:41:35PM +0100, Lo?c Hoguin wrote:
> Oh alright. Well they are equivalent URLs, so if you really need to
> redirect I would do so in the handler directly, or through a request
> hook.

I suppose this suggests my next question ... how would one accomplish
this right now in Cowboy? I hope I'm correct in assuming that this will
involve cowboy_dispatcher:match/3 in an onrequest fun, but I do not
understand how I will use match/3 with what I get from
cowboy_req:path/1.

Thomas


From essen at ninenines.eu  Tue Nov 13 16:22:48 2012
From: essen at ninenines.eu (=?ISO-8859-1?Q?Lo=EFc_Hoguin?=)
Date: Tue, 13 Nov 2012 16:22:48 +0100
Subject: [99s-extend] Proposal for Cowboy Routing
In-Reply-To: <[email protected]>
References: <[email protected]>
 <[email protected]> <[email protected]>
 <[email protected]> <[email protected]>
 <[email protected]>
Message-ID: <[email protected]>

On 11/13/2012 04:12 PM, Thomas Allen wrote:
> On Tue, Nov 13, 2012 at 03:41:35PM +0100, Lo?c Hoguin wrote:
>> Oh alright. Well they are equivalent URLs, so if you really need to
>> redirect I would do so in the handler directly, or through a request
>> hook.
>
> I suppose this suggests my next question ... how would one accomplish
> this right now in Cowboy? I hope I'm correct in assuming that this will
> involve cowboy_dispatcher:match/3 in an onrequest fun, but I do not
> understand how I will use match/3 with what I get from
> cowboy_req:path/1.

No, just check that cowboy_req:path/1 ends with $/, and if it doesn't 
then redirect.

-- 
Lo?c Hoguin
Erlang Cowboy
Nine Nines
http://ninenines.eu


From dch at jsonified.com  Tue Nov 13 16:42:53 2012
From: dch at jsonified.com (Dave Cottlehuber)
Date: Tue, 13 Nov 2012 16:42:53 +0100
Subject: [99s-extend] Proposal for Cowboy Routing
In-Reply-To: <[email protected]>
References: <[email protected]>
Message-ID: <CAL+Y1nunQttGU4=nTQkUZ6tfx7xAi+X4aDJeWAAHnFKsC3C81w@mail.gmail.com>

On 13 November 2012 10:04, Lo?c Hoguin <essen at ninenines.eu> wrote:
> Hello!
>
> I have put my thoughts on routing at the following gist:
>
>   https://gist.github.com/4064759
>
> Please comment or cry in terror if you're against it!
>
> Thanks.
>
> --
> Lo?c Hoguin
> Erlang Cowboy
> Nine Nines
> http://ninenines.eu
> _______________________________________________
> Extend mailing list
> Extend at lists.ninenines.eu
> http://lists.ninenines.eu:81/listinfo/extend

I actually commented on the gist, here's a copy for the ML:

== dch ==

Looks very useful for me. I've not heard of imbrication until today, I
assume it is closest to nested optional components as used above.

Having regex capability in the constraints would be great:

{"/cars/:name/:color", [{color, in, [blue, red, pink]}], Handler,
Opts} might be {"/cars/:name/:color", [{color, regex, "^(blue | red |
pink)$", [other_re_parameters]) , Handler, Opts} for example.

== Lo?c ==

Yes, regex will be a possible constraint. Also checking for integer
value. Not sure what else we'll want.

A+
Dave


From essen at ninenines.eu  Tue Nov 13 16:46:15 2012
From: essen at ninenines.eu (=?ISO-8859-1?Q?Lo=EFc_Hoguin?=)
Date: Tue, 13 Nov 2012 16:46:15 +0100
Subject: [99s-extend] Proposal for Cowboy Routing
In-Reply-To: <CAL+Y1nunQttGU4=nTQkUZ6tfx7xAi+X4aDJeWAAHnFKsC3C81w@mail.gmail.com>
References: <[email protected]>
 <CAL+Y1nunQttGU4=nTQkUZ6tfx7xAi+X4aDJeWAAHnFKsC3C81w@mail.gmail.com>
Message-ID: <[email protected]>

On 11/13/2012 04:42 PM, Dave Cottlehuber wrote:
> On 13 November 2012 10:04, Lo?c Hoguin <essen at ninenines.eu> wrote:
>> Hello!
>>
>> I have put my thoughts on routing at the following gist:
>>
>>    https://gist.github.com/4064759
>>
>> Please comment or cry in terror if you're against it!
>>
>> Thanks.
>>
>> --
>> Lo?c Hoguin
>> Erlang Cowboy
>> Nine Nines
>> http://ninenines.eu
>> _______________________________________________
>> Extend mailing list
>> Extend at lists.ninenines.eu
>> http://lists.ninenines.eu:81/listinfo/extend
>
> I actually commented on the gist, here's a copy for the ML:
>
> == dch ==
>
> Looks very useful for me. I've not heard of imbrication until today, I
> assume it is closest to nested optional components as used above.
>
> Having regex capability in the constraints would be great:
>
> {"/cars/:name/:color", [{color, in, [blue, red, pink]}], Handler,
> Opts} might be {"/cars/:name/:color", [{color, regex, "^(blue | red |
> pink)$", [other_re_parameters]) , Handler, Opts} for example.
>
> == Lo?c ==
>
> Yes, regex will be a possible constraint. Also checking for integer
> value. Not sure what else we'll want.

I'll add to that that in the case of integer, we could very well bind 
the value as an integer instead of as a binary, and avoid all these 
pesky list_to_integer(binary_to_list(Value)).

-- 
Lo?c Hoguin
Erlang Cowboy
Nine Nines
http://ninenines.eu


From thomas at oinksoft.com  Tue Nov 13 17:10:04 2012
From: thomas at oinksoft.com (Thomas Allen)
Date: Tue, 13 Nov 2012 11:10:04 -0500
Subject: [99s-extend] Proposal for Cowboy Routing
In-Reply-To: <[email protected]>
References: <[email protected]>
 <[email protected]>
 <[email protected]>
 <[email protected]>
 <[email protected]>
 <[email protected]>
 <[email protected]>
Message-ID: <[email protected]>

On Tue, Nov 13, 2012 at 04:22:48PM +0100, Lo?c Hoguin wrote:
> No, just check that cowboy_req:path/1 ends with $/, and if it
> doesn't then redirect.

OK, so what I'm going for is to actually check for a valid URL with the
slash. I got it to work in current cowboy, but shield your eyes ...

I do this "middleware" thing often ... this might be a misnomer as I
think some definitions of middleware specify that it be able to wrap the
request *and* the response. I digress ...

    %%% foo_app.erl:

    {ok, _} = cowboy:start_http(http, ?ACCEPTORS,
        [{port, ?PORT}],
        [{dispatch, Dispatch},
         {onrequest, fun(Req) ->
             foo_middleware:all(Dispatch, Req)
          end}]
    ),


    %% foo_middleware.erl:

    -define(MIDDLEWARE, [
        fun foo_middleware:slash/2,
        fun foo_middleware:session/2,
        fun foo_middleware:user/2
    ]).

    all(Dispatch, Req) ->
        lists:foldl(fun(F, CurReq) -> F(Dispatch, CurReq) end, Req,
            ?MIDDLEWARE).

    %% ...

    slash(Dispatch, Req) ->
        {Path, _} = cowboy_req:path(Req),
        case binary:last(Path) of
            $/ -> Req;
            _ ->
                SlashPath = <<Path/bitstring, "/">>,
                {Host, _} = cowboy_req:host(Req),
                Match = cowboy_dispatcher:match(Dispatch, Host,
                    SlashPath),
                case element(1, Match) of
                    ok ->
                        cowboy_req:reply(301,
                            [{<<"Location">>, SlashPath}], <<>>, Req);
                    error -> Req
                end
        end.

So, I assume that any path not ending in "/" won't match. Another
version of this could toss that assumption, but then you're performing
an extra match on every request ... much better to use a '_' rule in
that case.

One thing that is icky about using a fallback '_' handler, and the
reason I've elected to use onrequest for this example, is that I do not
see a way to access cowboy_protocol's state.dispatch to access these
rules. So, I enclose them in my `onrequest` fun. Would it be possible to
expose Dispatch? This has many applications, particularly for URL
reversing.

Perhaps I am missing several things ;^)

Thomas


From essen at ninenines.eu  Thu Nov 29 11:46:10 2012
From: essen at ninenines.eu (=?ISO-8859-1?Q?Lo=EFc_Hoguin?=)
Date: Thu, 29 Nov 2012 11:46:10 +0100
Subject: [99s-extend] Proposal for Cowboy Routing
In-Reply-To: <[email protected]>
References: <[email protected]>
Message-ID: <[email protected]>

On 11/13/2012 10:04 AM, Lo?c Hoguin wrote:
> Hello!
>
> I have put my thoughts on routing at the following gist:
>
>    https://gist.github.com/4064759
>
> Please comment or cry in terror if you're against it!

I have added more infos and changes as you can see in the comments here: 
https://gist.github.com/4064759#comments Please feedback.

-- 
Lo?c Hoguin
Erlang Cowboy
Nine Nines
http://ninenines.eu