Permalink
Browse files

sync with internal version

  • Loading branch information...
1 parent 21240b8 commit 2b76820fa2c72217490c8364c88b7a2856b32667 @etrepum etrepum committed Feb 14, 2008
Showing with 99 additions and 90 deletions.
  1. +45 −68 src/mochijson.erl
  2. +20 −15 src/mochiweb.app
  3. +1 −1 src/mochiweb_cookies.erl
  4. +1 −1 src/mochiweb_http.erl
  5. +13 −5 src/mochiweb_request.erl
  6. +19 −0 src/mochiweb_util.erl
View
@@ -148,7 +148,7 @@ json_encode_proplist(Props, State) ->
F = fun ({K, V}, Acc) ->
KS = case K of
K when is_atom(K) ->
- json_encode_string_utf8(atom_to_list(K), [?Q]);
+ json_encode_string_utf8(atom_to_list(K));
K when is_integer(K) ->
json_encode_string(integer_to_list(K), State);
K when is_list(K); is_binary(K) ->
@@ -161,76 +161,53 @@ json_encode_proplist(Props, State) ->
lists:reverse([$\} | Acc1]).
json_encode_string(A, _State) when is_atom(A) ->
- json_encode_string_unicode(xmerl_ucs:from_utf8(atom_to_list(A)), [?Q]);
+ json_encode_string_unicode(xmerl_ucs:from_utf8(atom_to_list(A)));
json_encode_string(B, _State) when is_binary(B) ->
- json_encode_string_unicode(xmerl_ucs:from_utf8(B), [?Q]);
+ json_encode_string_unicode(xmerl_ucs:from_utf8(B));
json_encode_string(S, #encoder{input_encoding=utf8}) ->
- json_encode_string_utf8(S, [?Q]);
+ json_encode_string_utf8(S);
json_encode_string(S, #encoder{input_encoding=unicode}) ->
- json_encode_string_unicode(S, [?Q]).
-
-json_encode_string_utf8([], Acc) ->
- lists:reverse([$\" | Acc]);
-json_encode_string_utf8(All=[C | Cs], Acc) ->
- case C of
- C when C >= 16#7f ->
- json_encode_string_unicode(xmerl_ucs:from_utf8(All), Acc);
- _ ->
- Acc1 = case C of
- ?Q ->
- [?Q, $\\ | Acc];
- $/ ->
- [$/, $\\ | Acc];
- $\\ ->
- [$\\, $\\ | Acc];
- $\b ->
- [$b, $\\ | Acc];
- $\f ->
- [$f, $\\ | Acc];
- $\n ->
- [$n, $\\ | Acc];
- $\r ->
- [$r, $\\ | Acc];
- $\t ->
- [$t, $\\ | Acc];
- C when C >= 0, C < $\s ->
- [unihex(C) | Acc];
- C when C >= $\s ->
- [C | Acc];
- _ ->
- exit({json_encode, {bad_char, C}})
- end,
- json_encode_string_utf8(Cs, Acc1)
- end.
-
-json_encode_string_unicode([], Acc) ->
- lists:reverse([$\" | Acc]);
-json_encode_string_unicode([C | Cs], Acc) ->
- Acc1 = case C of
- ?Q ->
- [?Q, $\\ | Acc];
- $/ ->
- [$/, $\\ | Acc];
- $\\ ->
- [$\\, $\\ | Acc];
- $\b ->
- [$b, $\\ | Acc];
- $\f ->
- [$f, $\\ | Acc];
- $\n ->
- [$n, $\\ | Acc];
- $\r ->
- [$r, $\\ | Acc];
- $\t ->
- [$t, $\\ | Acc];
- C when C >= 0, C < $\s; C >= 16#7f, C =< 16#10FFFF ->
- [unihex(C) | Acc];
- C when C < 16#7f ->
- [C | Acc];
- _ ->
- exit({json_encode, {bad_char, C}})
- end,
- json_encode_string_unicode(Cs, Acc1).
+ json_encode_string_unicode(S).
+
+json_encode_string_utf8(S) ->
+ [?Q | json_encode_string_utf8_1(S)].
+
+json_encode_string_utf8_1([C | Cs]) when C >= 0, C =< 16#7f ->
+ NewC = case C of
+ _ when C >= $\s, C < 16#7f -> C;
+ $\\ -> "\\\\";
+ $\t -> "\\t";
+ $\n -> "\\n";
+ $\r -> "\\r";
+ $\f -> "\\f";
+ $\b -> "\\b";
+ _ when C >= 0, C =< 16#7f -> unihex(C);
+ _ -> exit({json_encode, {bad_char, C}})
+ end,
+ [NewC | json_encode_string_utf8_1(Cs)];
+json_encode_string_utf8_1(All=[C | _]) when C >= 16#80, C =< 16#10FFFF ->
+ json_encode_string_unicode(xmerl_ucs:from_utf8(All));
+json_encode_string_utf8_1([]) ->
+ "\"".
+
+json_encode_string_unicode(S) ->
+ [?Q | json_encode_string_unicode_1(S)].
+
+json_encode_string_unicode_1([C | Cs]) ->
+ NewC = case C of
+ _ when C >= $\s, C < 16#7f -> C;
+ $\\ -> "\\\\";
+ $\t -> "\\t";
+ $\n -> "\\n";
+ $\r -> "\\r";
+ $\f -> "\\f";
+ $\b -> "\\b";
+ _ when C >= 0, C =< 16#10FFFF -> unihex(C);
+ _ -> exit({json_encode, {bad_char, C}})
+ end,
+ [NewC | json_encode_string_unicode_1(Cs)];
+json_encode_string_unicode_1([]) ->
+ "\"".
dehex(C) when C >= $0, C =< $9 ->
C - $0;
View
@@ -2,21 +2,26 @@
[{description, "MochiMedia Web Server"},
{vsn, "0.01"},
{modules, [
- mochihex,
- mochijson,
- mochijson2,
- mochiweb,
- mochiweb_app,
- mochiweb_cookies,
- mochiweb_echo,
- mochiweb_headers,
- mochiweb_http,
- mochiweb_multipart,
- mochiweb_request,
- mochiweb_response,
- mochiweb_socket_server,
- mochiweb_sup,
- mochiweb_util
+ mochihex,
+ mochijson,
+ mochijson2,
+ mochinum,
+ mochiweb,
+ mochiweb_app,
+ mochiweb_charref,
+ mochiweb_cookies,
+ mochiweb_echo,
+ mochiweb_headers,
+ mochiweb_html,
+ mochiweb_http,
+ mochiweb_multipart,
+ mochiweb_request,
+ mochiweb_response,
+ mochiweb_skel,
+ mochiweb_socket_server,
+ mochiweb_sup,
+ mochiweb_util,
+ reloader
]},
{registered, []},
{mod, {mochiweb_app, []}},
View
@@ -105,7 +105,7 @@ cookie(Key, Value, Options) ->
quote(V0) ->
V = any_to_list(V0),
lists:all(fun(Ch) -> Ch =:= $/ orelse not ?IS_SEPARATOR(Ch) end, V)
- orelse erlang:error(cookie_quoting_required),
+ orelse erlang:error({cookie_quoting_required, V}),
V.
add_seconds(Secs, LocalTime) ->
View
@@ -72,7 +72,7 @@ default_body(Req, M, _Path) when M =:= 'GET'; M =:= 'HEAD' ->
{parse_cookie, Req:parse_cookie()},
Req:dump()]]),
Req:ok({"text/html",
- [mochiweb_cookies:cookie("mochiweb_http", "test cookie")],
+ [mochiweb_cookies:cookie("mochiweb_http", "test_cookie")],
frm(Body)});
default_body(Req, 'POST', "/multipart") ->
Body = io_lib:format("~p~n", [[{parse_qs, Req:parse_qs()},
View
@@ -96,7 +96,7 @@ get(range) ->
undefined ->
undefined;
RawRange ->
- parse_range_request(RawRange)
+ parse_range_request(RawRange)
end.
%% @spec dump() -> {mochiweb_request, [{atom(), term()}]}
@@ -164,6 +164,12 @@ recv_body() ->
%% @doc Receive the body of the HTTP request (defined by Content-Length).
%% Will receive up to MaxBody bytes.
recv_body(MaxBody) ->
+ case get_header_value("expect") of
+ "100-continue" ->
+ start_raw_response({100, gb_trees:empty()});
+ _Else ->
+ ok
+ end,
Body = case body_length() of
undefined ->
undefined;
@@ -192,7 +198,7 @@ start_response({Code, ResponseHeaders}) ->
HResponse),
start_raw_response({Code, HResponse1}).
-%% @spec start_raw_response({integer(), ioheaders()}) -> response()
+%% @spec start_raw_response({integer(), headers()}) -> response()
%% @doc Start the HTTP response by sending the Code HTTP response and
%% ResponseHeaders.
start_raw_response({Code, ResponseHeaders}) ->
@@ -429,7 +435,9 @@ serve_file(Path, DocRoot) ->
case file:open(File, [raw, binary]) of
{ok, IoDevice} ->
ContentType = mochiweb_util:guess_mime(File),
- ok({ContentType, {file, IoDevice}});
+ Res = ok({ContentType, {file, IoDevice}}),
+ file:close(IoDevice),
+ Res;
_ ->
not_found()
end;
@@ -529,7 +537,7 @@ range_parts(Body0, Ranges) ->
Size = size(Body),
F = fun(Spec, Acc) ->
case range_skip_length(Spec, Size) of
- invalid ->
+ invalid_range ->
Acc;
{Skip, Length} ->
<<_:Skip/binary, PartialBody:Length/binary, _/binary>> = Body,
@@ -619,7 +627,7 @@ test_range() ->
Body = <<"012345678901234567890123456789012345678901234567890123456789">>,
BodySize = size(Body), %% 60
- BodySize =:= 60,
+ BodySize = 60,
%% these values assume BodySize =:= 60
io:format("Testing out range_skip_length on valid ranges~n"),
View
@@ -10,6 +10,7 @@
-export([urlsplit/1, urlsplit_path/1, urlunsplit/1, urlunsplit_path/1]).
-export([guess_mime/1, parse_header/1]).
-export([shell_quote/1, cmd/1, cmd_string/1, cmd_port/2]).
+-export([record_to_proplist/2, record_to_proplist/3]).
-export([test/0]).
-define(PERCENT, 37). % $\%
@@ -311,6 +312,24 @@ unquote_header([$\\, C | Rest], Acc) ->
unquote_header([C | Rest], Acc) ->
unquote_header(Rest, [C | Acc]).
+%% @spec record_to_proplist(Record, Fields) -> proplist()
+%% @doc calls record_to_proplist/3 with a default TypeKey of '__record'
+record_to_proplist(Record, Fields) ->
+ record_to_proplist(Record, Fields, '__record').
+
+%% @spec record_to_proplist(Record, Fields, TypeKey) -> proplist()
+%% @doc Return a proplist of the given Record with each field in the
+%% Fields list set as a key with the corresponding value in the Record.
+%% TypeKey is the key that is used to store the record type
+%% Fields should be obtained by calling record_info(fields, record_type)
+%% where record_type is the record type of Record
+record_to_proplist(Record, Fields, TypeKey)
+ when is_tuple(Record),
+ is_list(Fields),
+ size(Record) - 1 =:= length(Fields) ->
+ lists:zip([TypeKey | Fields], tuple_to_list(Record)).
+
+
shell_quote([], Acc) ->
lists:reverse([$\" | Acc]);
shell_quote([C | Rest], Acc) when C =:= $\" orelse C =:= $\` orelse

0 comments on commit 2b76820

Please sign in to comment.