summaryrefslogtreecommitdiffstats
path: root/archives/extend/2014-September.txt
blob: 58303855a4450138bef80169c1d0860d6aeaeef0 (plain) (blame)
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
From paulo.ferraz.oliveira at gmail.com  Mon Sep 15 23:24:20 2014
From: paulo.ferraz.oliveira at gmail.com (Paulo F. Oliveira)
Date: Mon, 15 Sep 2014 22:24:20 +0100
Subject: [99s-extend] Using cowboy_req:body more than once per request
Message-ID: <CA+dV7cT+POGJM=nqBcvnP_NoZUa=_ZHjGx-9gBQ+09Pe7aE2bg@mail.gmail.com>

Hi.

I recently implemented a checksum header (X-Checksum) that allows
validating the content of a request's body by hash comparison (just to give
you some context). I'm using the onrequest hook to affect all requests (and
be able to reply appropriately for non-conformance to the hash function
result) but can't figure out how to not read the request body twice, i.e. I
read it in the onrequest hook but later on need to read it again in the
route handler, but I can't (from the manual, for cowboy_req:body: "This
function can only be called once. Cowboy will not cache the result of this
call."). At the moment, and because the API consumers were in a hurry, the
solution I found (I understand it might be an ugly hack), was to read the
body, store it in the Req's meta (property body, for example) and then
access that property later on, instead of using cowboy_req:body. I'm not
quite happy with this solution and was wondering if there is anything more
elegant that I can implement.

Thanks.

Cheers.

- Paulo F. Oliveira
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ninenines.eu/archives/extend/attachments/20140915/26d4e023/attachment-0001.html>

From paulo.ferraz.oliveira at gmail.com  Mon Sep 15 23:34:48 2014
From: paulo.ferraz.oliveira at gmail.com (Paulo F. Oliveira)
Date: Mon, 15 Sep 2014 22:34:48 +0100
Subject: [99s-extend] :binding doc
Message-ID: <CA+dV7cTDYsbcVjwO3wxNpzpo0V3gcO+XoY44Yjbc6K1OB8zuVw@mail.gmail.com>

Hi.

This can be read in the cowboy_req:binding doc: "By default the value is a
binary, however constraints may change the type of this value (for example
automatically converting numbers to integer)."

What constraints are we talking about here?

Also, there's no reference to the fact that the bindings are URL-decoded,
even though they appear to be.

Cheers.

- Paulo F. Oliveira
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ninenines.eu/archives/extend/attachments/20140915/5f3302e4/attachment.html>

From paulo.ferraz.oliveira at gmail.com  Mon Sep 15 23:55:39 2014
From: paulo.ferraz.oliveira at gmail.com (Paulo F. Oliveira)
Date: Mon, 15 Sep 2014 22:55:39 +0100
Subject: [99s-extend] :binding doc
Message-ID: <CA+dV7cQkY9k0RSaR+_ja1MQae=f-Yb2Wp7aFGok3ORA=jSE+aA@mail.gmail.com>

OK, I guess "constraints" refers to this:
http://ninenines.eu/docs/en/cowboy/HEAD/guide/routing/#constraints

:D

Cheers.

- Paulo F. Oliveira
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ninenines.eu/archives/extend/attachments/20140915/d97a6072/attachment.html>

From essen at ninenines.eu  Tue Sep 16 00:15:44 2014
From: essen at ninenines.eu (=?UTF-8?B?TG/Dr2MgSG9ndWlu?=)
Date: Tue, 16 Sep 2014 00:15:44 +0200
Subject: [99s-extend] :binding doc
In-Reply-To: <CA+dV7cTDYsbcVjwO3wxNpzpo0V3gcO+XoY44Yjbc6K1OB8zuVw@mail.gmail.com>
References: <CA+dV7cTDYsbcVjwO3wxNpzpo0V3gcO+XoY44Yjbc6K1OB8zuVw@mail.gmail.com>
Message-ID: <[email protected]>

On 09/15/2014 11:34 PM, Paulo F. Oliveira wrote:
> Also, there's no reference to the fact that the bindings are
> URL-decoded, even though they appear to be.

Cowboy decodes everything. If you feel it's helpful to mention, please 
send a patch.

-- 
Lo?c Hoguin
http://ninenines.eu

From essen at ninenines.eu  Tue Sep 16 00:22:27 2014
From: essen at ninenines.eu (=?UTF-8?B?TG/Dr2MgSG9ndWlu?=)
Date: Tue, 16 Sep 2014 00:22:27 +0200
Subject: [99s-extend] Using cowboy_req:body more than once per request
In-Reply-To: <CA+dV7cT+POGJM=nqBcvnP_NoZUa=_ZHjGx-9gBQ+09Pe7aE2bg@mail.gmail.com>
References: <CA+dV7cT+POGJM=nqBcvnP_NoZUa=_ZHjGx-9gBQ+09Pe7aE2bg@mail.gmail.com>
Message-ID: <[email protected]>

It seems a bit weird to me to read the body and validate it before 
validating the request itself.

I would explicitly put these checks in the handler directly. This of 
course means that there is no need to read it twice anymore.

On 09/15/2014 11:24 PM, Paulo F. Oliveira wrote:
> Hi.
>
> I recently implemented a checksum header (X-Checksum) that allows
> validating the content of a request's body by hash comparison (just to
> give you some context). I'm using the onrequest hook to affect all
> requests (and be able to reply appropriately for non-conformance to the
> hash function result) but can't figure out how to not read the request
> body twice, i.e. I read it in the onrequest hook but later on need to
> read it again in the route handler, but I can't (from the manual, for
> cowboy_req:body: "This function can only be called once. Cowboy will not
> cache the result of this call."). At the moment, and because the API
> consumers were in a hurry, the solution I found (I understand it might
> be an ugly hack), was to read the body, store it in the Req's meta
> (property body, for example) and then access that property later on,
> instead of using cowboy_req:body. I'm not quite happy with this solution
> and was wondering if there is anything more elegant that I can implement.
>
> Thanks.
>
> Cheers.
>
> - Paulo F. Oliveira
>
>
> _______________________________________________
> Extend mailing list
> Extend at lists.ninenines.eu
> https://lists.ninenines.eu/listinfo/extend
>

-- 
Lo?c Hoguin
http://ninenines.eu

From paulo.ferraz.oliveira at gmail.com  Tue Sep 16 00:35:20 2014
From: paulo.ferraz.oliveira at gmail.com (Paulo F. Oliveira)
Date: Mon, 15 Sep 2014 23:35:20 +0100
Subject: [99s-extend] Using cowboy_req:body more than once per request
In-Reply-To: <[email protected]>
References: <CA+dV7cT+POGJM=nqBcvnP_NoZUa=_ZHjGx-9gBQ+09Pe7aE2bg@mail.gmail.com>
 <[email protected]>
Message-ID: <CA+dV7cSQc=3wsQrYkg2p6R9M7n_HmZpF6kOt=4CbaphUUx4oGQ@mail.gmail.com>

Hi.

> It seems a bit weird to me to read the body and validate it before validating the request itself.

It certainly seems like it, but I had no immediate solution and
instead of changing a dozen handlers, this seemed faster to implement
:D. I don't understand what you mean by "validating the request
itself". I read the header (I mentioned previously) and the body and
check one against the other. They are present and enough for the
_validator_ to make a decision, but I might be missing something here.

> I would explicitly put these checks in the handler directly. This of course means that there is no need to read it twice anymore.

I've been trying to find a way to easily share code between handlers
without having to rewrite a lot of code (even if I do decide to put
things in a library function - or several). I recently came across
https://github.com/opscode/mixer. Have you ever used it?

Thanks.

- Paulo F. Oliveira

From essen at ninenines.eu  Tue Sep 16 00:42:20 2014
From: essen at ninenines.eu (=?UTF-8?B?TG/Dr2MgSG9ndWlu?=)
Date: Tue, 16 Sep 2014 00:42:20 +0200
Subject: [99s-extend] Using cowboy_req:body more than once per request
In-Reply-To: <CA+dV7cSQc=3wsQrYkg2p6R9M7n_HmZpF6kOt=4CbaphUUx4oGQ@mail.gmail.com>
References: <CA+dV7cT+POGJM=nqBcvnP_NoZUa=_ZHjGx-9gBQ+09Pe7aE2bg@mail.gmail.com>	<[email protected]>
 <CA+dV7cSQc=3wsQrYkg2p6R9M7n_HmZpF6kOt=4CbaphUUx4oGQ@mail.gmail.com>
Message-ID: <[email protected]>

On 09/16/2014 12:35 AM, Paulo F. Oliveira wrote:
> Hi.
>
>> It seems a bit weird to me to read the body and validate it before validating the request itself.
>
> It certainly seems like it, but I had no immediate solution and
> instead of changing a dozen handlers, this seemed faster to implement
> :D. I don't understand what you mean by "validating the request
> itself". I read the header (I mentioned previously) and the body and
> check one against the other. They are present and enough for the
> _validator_ to make a decision, but I might be missing something here.

Like, is it the right method? Are the bindings/qs parameters/headers 
present and valid? And so on. The body should be the last thing you 
check, due to how expensive it can be, not the first.

>> I would explicitly put these checks in the handler directly. This of course means that there is no need to read it twice anymore.
>
> I've been trying to find a way to easily share code between handlers
> without having to rewrite a lot of code (even if I do decide to put
> things in a library function - or several). I recently came across
> https://github.com/opscode/mixer. Have you ever used it?

I usually share code by writing functions. Then I call these functions 
where needed.

-- 
Lo?c Hoguin
http://ninenines.eu

From jmrepetti at gmail.com  Mon Sep 29 18:52:16 2014
From: jmrepetti at gmail.com (=?UTF-8?B?SnVhbiBNYXTDrWFz?=)
Date: Mon, 29 Sep 2014 18:52:16 +0200
Subject: [99s-extend] Newbie, Cowboy + Websocket + Audio Recording
Message-ID: <CA+ycm=6T4gu=48=VfP3aiVdJFAg--s+xGaT2RtGm1KU8qyLiTw@mail.gmail.com>

Hello list, I hope this is the right place to ask this.

I'm learning Erlang, and I wanted to create a Cowboy app to record audio
from a web browser.

Based on the websocket example in the Cowboy source code, I get the user
mic input and send this input to the websocket.

I created a "recorder" module, which functionality is to save the data to
the a file.


*#rawe_handler.erl*-module(rawec_handler).
-behaviour(cowboy_websocket_handler).
......
init(_, _, _) ->
  case whereis(recorder) of
    undefined ->
        RecorderPid = recorder:start(),
        register(recorder, RecorderPid);
    _ -> ok
  end,
    {upgrade, protocol, cowboy_websocket}.
.....
websocket_handle(_Frame, Req, State) ->
  RecorderPid = whereis(recorder),
  RecorderPid ! {rec, _Frame/binary},
    {ok, Req, State}.

*#recorder.erl*
-module(recorder).

-export([start/0, recorder_fun/1]).
-compile([debug_info]).

recorder_fun(IoDevice) ->
  receive
    {rec, Data} ->
      ok = file:write(IoDevice, Data),
      io:format(Data),
      recorder_fun(IoDevice);
    {stop, _} ->
      %%Close file
      file:close(IoDevice)
    end.

start() ->
  {ok, IoDevice} = file:open("/tmp/test_binary.wav", [write,
binary]),
  spawn(recorder, recorder_fun, [IoDevice]).


When I start the console, and allow the microphone on the browser, I see
this error on the console:

=ERROR REPORT==== 29-Sep-2014::18:13:03 ===
Ranch listener http had connection process started with
cowboy_protocol:start_link/4 at <0.178.0> exit with reason:
*{[{reason,badarith},{mfa,{rawec_handler,websocket_handle,3*}},{stacktrace,[{rawec_handler,websocket_handle,3,[{file,"src/rawec_handler.erl"},{line,35}]},{cowboy_websocket,handler_call,7,[{file,"src/cowboy_websocket.erl"},{line,588}]},{cowboy_protocol,execute,4,[{file,"src/cowboy_protocol.erl"},{line,435}]}]},{msg,{binary,<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0....(ETC,
DATA STREAM CONTINUES)

Probably my approach to do this is totally wrong. I there any obvious
problem here?
Can someone point me to a right direction?. Maybe I should write directly
to a file in the *websocket_handle *funcion, but how can I keep a file
opened during the streaming?

The github repo is here: https://github.com/jmrepetti/rawec with the whole
source code if you want to take a look.


Thanks in advance,
Matias.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ninenines.eu/archives/extend/attachments/20140929/84fe21a4/attachment.html>

From edgurgel at gmail.com  Tue Sep 30 00:53:26 2014
From: edgurgel at gmail.com (Eduardo Gurgel)
Date: Tue, 30 Sep 2014 11:53:26 +1300
Subject: [99s-extend] Newbie, Cowboy + Websocket + Audio Recording
In-Reply-To: <CA+ycm=6T4gu=48=VfP3aiVdJFAg--s+xGaT2RtGm1KU8qyLiTw@mail.gmail.com>
References: <CA+ycm=6T4gu=48=VfP3aiVdJFAg--s+xGaT2RtGm1KU8qyLiTw@mail.gmail.com>
Message-ID: <CAKAMJXghRnTZ1CTPkTBapOgh7BnFLUnvL++VVedyXuAzWT6HSQ@mail.gmail.com>

Looking on the output it says:

*{reason,badarith} *on this line:

RecorderPid ! {rec, _Frame/binary},

This may help you somehow.

BTW, variables starting with _ are usually used to show unused variables
and stop warnings from the compiler.

On 30 September 2014 05:52, Juan Mat?as <jmrepetti at gmail.com> wrote:

> Hello list, I hope this is the right place to ask this.
>
> I'm learning Erlang, and I wanted to create a Cowboy app to record audio
> from a web browser.
>
> Based on the websocket example in the Cowboy source code, I get the user
> mic input and send this input to the websocket.
>
> I created a "recorder" module, which functionality is to save the data to
> the a file.
>
>
> *#rawe_handler.erl*-module(rawec_handler).
> -behaviour(cowboy_websocket_handler).
> ......
> init(_, _, _) ->
>   case whereis(recorder) of
>     undefined ->
>         RecorderPid = recorder:start(),
>         register(recorder, RecorderPid);
>     _ -> ok
>   end,
>     {upgrade, protocol, cowboy_websocket}.
> .....
> websocket_handle(_Frame, Req, State) ->
>   RecorderPid = whereis(recorder),
>   RecorderPid ! {rec, _Frame/binary},
>     {ok, Req, State}.
>
> *#recorder.erl*
> -module(recorder).
>
> -export([start/0, recorder_fun/1]).
> -compile([debug_info]).
>
> recorder_fun(IoDevice) ->
>   receive
>     {rec, Data} ->
>       ok = file:write(IoDevice, Data),
>       io:format(Data),
>       recorder_fun(IoDevice);
>     {stop, _} ->
>       %%Close file
>       file:close(IoDevice)
>     end.
>
> start() ->
>   {ok, IoDevice} = file:open("/tmp/test_binary.wav", [write,
> binary]),
>   spawn(recorder, recorder_fun, [IoDevice]).
>
>
> When I start the console, and allow the microphone on the browser, I see
> this error on the console:
>
> =ERROR REPORT==== 29-Sep-2014::18:13:03 ===
> Ranch listener http had connection process started with
> cowboy_protocol:start_link/4 at <0.178.0> exit with reason:
> *{[{reason,badarith},{mfa,{rawec_handler,websocket_handle,3*}},{stacktrace,[{rawec_handler,websocket_handle,3,[{file,"src/rawec_handler.erl"},{line,35}]},{cowboy_websocket,handler_call,7,[{file,"src/cowboy_websocket.erl"},{line,588}]},{cowboy_protocol,execute,4,[{file,"src/cowboy_protocol.erl"},{line,435}]}]},{msg,{binary,<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0....(ETC,
> DATA STREAM CONTINUES)
>
> Probably my approach to do this is totally wrong. I there any obvious
> problem here?
> Can someone point me to a right direction?. Maybe I should write directly
> to a file in the *websocket_handle *funcion, but how can I keep a file
> opened during the streaming?
>
> The github repo is here: https://github.com/jmrepetti/rawec with the
> whole source code if you want to take a look.
>
>
> Thanks in advance,
> Matias.
>
> _______________________________________________
> Extend mailing list
> Extend at lists.ninenines.eu
> https://lists.ninenines.eu/listinfo/extend
>
>


-- 
Eduardo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ninenines.eu/archives/extend/attachments/20140930/6d952ce6/attachment-0001.html>

From jmrepetti at gmail.com  Tue Sep 30 12:38:15 2014
From: jmrepetti at gmail.com (=?UTF-8?B?SnVhbiBNYXTDrWFz?=)
Date: Tue, 30 Sep 2014 12:38:15 +0200
Subject: [99s-extend] Newbie, Cowboy + Websocket + Audio Recording
In-Reply-To: <CAKAMJXghRnTZ1CTPkTBapOgh7BnFLUnvL++VVedyXuAzWT6HSQ@mail.gmail.com>
References: <CA+ycm=6T4gu=48=VfP3aiVdJFAg--s+xGaT2RtGm1KU8qyLiTw@mail.gmail.com>
 <CAKAMJXghRnTZ1CTPkTBapOgh7BnFLUnvL++VVedyXuAzWT6HSQ@mail.gmail.com>
Message-ID: <CA+ycm=5YMMKFpZnfGrOBLu5Jn4WFAiX7BmwbKBti2YsVmJ5JjQ@mail.gmail.com>

Thanks, that fixed the error. Now I'm having other but I'll investigate.



On Tue, Sep 30, 2014 at 12:53 AM, Eduardo Gurgel <edgurgel at gmail.com> wrote:

> Looking on the output it says:
>
> *{reason,badarith} *on this line:
>
> RecorderPid ! {rec, _Frame/binary},
>
> This may help you somehow.
>
> BTW, variables starting with _ are usually used to show unused variables
> and stop warnings from the compiler.
>
> On 30 September 2014 05:52, Juan Mat?as <jmrepetti at gmail.com> wrote:
>
>> Hello list, I hope this is the right place to ask this.
>>
>> I'm learning Erlang, and I wanted to create a Cowboy app to record audio
>> from a web browser.
>>
>> Based on the websocket example in the Cowboy source code, I get the user
>> mic input and send this input to the websocket.
>>
>> I created a "recorder" module, which functionality is to save the data to
>> the a file.
>>
>>
>> *#rawe_handler.erl*-module(rawec_handler).
>> -behaviour(cowboy_websocket_handler).
>> ......
>> init(_, _, _) ->
>>   case whereis(recorder) of
>>     undefined ->
>>         RecorderPid = recorder:start(),
>>         register(recorder, RecorderPid);
>>     _ -> ok
>>   end,
>>     {upgrade, protocol, cowboy_websocket}.
>> .....
>> websocket_handle(_Frame, Req, State) ->
>>   RecorderPid = whereis(recorder),
>>   RecorderPid ! {rec, _Frame/binary},
>>     {ok, Req, State}.
>>
>> *#recorder.erl*
>> -module(recorder).
>>
>> -export([start/0, recorder_fun/1]).
>> -compile([debug_info]).
>>
>> recorder_fun(IoDevice) ->
>>   receive
>>     {rec, Data} ->
>>       ok = file:write(IoDevice, Data),
>>       io:format(Data),
>>       recorder_fun(IoDevice);
>>     {stop, _} ->
>>       %%Close file
>>       file:close(IoDevice)
>>     end.
>>
>> start() ->
>>   {ok, IoDevice} = file:open("/tmp/test_binary.wav", [write,
>> binary]),
>>   spawn(recorder, recorder_fun, [IoDevice]).
>>
>>
>> When I start the console, and allow the microphone on the browser, I see
>> this error on the console:
>>
>> =ERROR REPORT==== 29-Sep-2014::18:13:03 ===
>> Ranch listener http had connection process started with
>> cowboy_protocol:start_link/4 at <0.178.0> exit with reason:
>> *{[{reason,badarith},{mfa,{rawec_handler,websocket_handle,3*}},{stacktrace,[{rawec_handler,websocket_handle,3,[{file,"src/rawec_handler.erl"},{line,35}]},{cowboy_websocket,handler_call,7,[{file,"src/cowboy_websocket.erl"},{line,588}]},{cowboy_protocol,execute,4,[{file,"src/cowboy_protocol.erl"},{line,435}]}]},{msg,{binary,<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0....(ETC,
>> DATA STREAM CONTINUES)
>>
>> Probably my approach to do this is totally wrong. I there any obvious
>> problem here?
>> Can someone point me to a right direction?. Maybe I should write directly
>> to a file in the *websocket_handle *funcion, but how can I keep a file
>> opened during the streaming?
>>
>> The github repo is here: https://github.com/jmrepetti/rawec with the
>> whole source code if you want to take a look.
>>
>>
>> Thanks in advance,
>> Matias.
>>
>> _______________________________________________
>> Extend mailing list
>> Extend at lists.ninenines.eu
>> https://lists.ninenines.eu/listinfo/extend
>>
>>
>
>
> --
> Eduardo
>



-- 
Mat?as
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ninenines.eu/archives/extend/attachments/20140930/ef46837f/attachment.html>