urlencoded.ex (1590B)
1 defmodule Plug.Parsers.URLENCODED do 2 @moduledoc """ 3 Parses urlencoded request body. 4 5 ## Options 6 7 All options supported by `Plug.Conn.read_body/2` are also supported here. 8 They are repeated here for convenience: 9 10 * `:length` - sets the maximum number of bytes to read from the request, 11 defaults to 1_000_000 bytes 12 * `:read_length` - sets the amount of bytes to read at one time from the 13 underlying socket to fill the chunk, defaults to 1_000_000 bytes 14 * `:read_timeout` - sets the timeout for each socket read, defaults to 15 15_000ms 16 17 So by default, `Plug.Parsers` will read 1_000_000 bytes at a time from the 18 socket with an overall limit of 8_000_000 bytes. 19 """ 20 21 @behaviour Plug.Parsers 22 23 @impl true 24 def init(opts) do 25 opts = Keyword.put_new(opts, :length, 1_000_000) 26 Keyword.pop(opts, :body_reader, {Plug.Conn, :read_body, []}) 27 end 28 29 @impl true 30 def parse(conn, "application", "x-www-form-urlencoded", _headers, {{mod, fun, args}, opts}) do 31 case apply(mod, fun, [conn, opts | args]) do 32 {:ok, body, conn} -> 33 validate_utf8 = Keyword.get(opts, :validate_utf8, true) 34 35 {:ok, 36 Plug.Conn.Query.decode( 37 body, 38 %{}, 39 Plug.Parsers.BadEncodingError, 40 validate_utf8 41 ), conn} 42 43 {:more, _data, conn} -> 44 {:error, :too_large, conn} 45 46 {:error, :timeout} -> 47 raise Plug.TimeoutError 48 49 {:error, _} -> 50 raise Plug.BadRequestError 51 end 52 end 53 54 def parse(conn, _type, _subtype, _headers, _opts) do 55 {:next, conn} 56 end 57 end