Add support for HTTP File Upload, version 0.3.0

Support the current XEP-0363 version in addition to the previous
revisions.
This commit is contained in:
Holger Weiss 2017-04-21 18:36:53 +02:00
parent 5444475b1d
commit 168712ebbd
2 changed files with 46 additions and 21 deletions

View File

@ -24,7 +24,7 @@
{fast_tls, ".*", {git, "https://github.com/processone/fast_tls", {tag, "1.0.11"}}}, {fast_tls, ".*", {git, "https://github.com/processone/fast_tls", {tag, "1.0.11"}}},
{stringprep, ".*", {git, "https://github.com/processone/stringprep", {tag, "1.0.8"}}}, {stringprep, ".*", {git, "https://github.com/processone/stringprep", {tag, "1.0.8"}}},
{fast_xml, ".*", {git, "https://github.com/processone/fast_xml", {tag, "1.1.21"}}}, {fast_xml, ".*", {git, "https://github.com/processone/fast_xml", {tag, "1.1.21"}}},
{xmpp, ".*", {git, "https://github.com/processone/xmpp", "806ac5e4ebfbcf1f8f5ebe5a8458ebb7e250d501"}}, {xmpp, ".*", {git, "https://github.com/processone/xmpp", "06d4d5720c2609c5b44ca59400fddcf944d6f3fa"}},
{stun, ".*", {git, "https://github.com/processone/stun", {tag, "1.0.10"}}}, {stun, ".*", {git, "https://github.com/processone/stun", {tag, "1.0.10"}}},
{esip, ".*", {git, "https://github.com/processone/esip", {tag, "1.0.11"}}}, {esip, ".*", {git, "https://github.com/processone/esip", {tag, "1.0.11"}}},
{fast_yaml, ".*", {git, "https://github.com/processone/fast_yaml", {tag, "1.0.9"}}}, {fast_yaml, ".*", {git, "https://github.com/processone/fast_yaml", {tag, "1.0.9"}}},

View File

@ -512,12 +512,31 @@ process_iq(#iq{type = get, lang = Lang, sub_els = [#disco_info{}]} = IQ,
xmpp:make_iq_result(IQ, iq_disco_info(ServerHost, Lang, Name, AddInfo)); xmpp:make_iq_result(IQ, iq_disco_info(ServerHost, Lang, Name, AddInfo));
process_iq(#iq{type = get, sub_els = [#disco_items{}]} = IQ, _State) -> process_iq(#iq{type = get, sub_els = [#disco_items{}]} = IQ, _State) ->
xmpp:make_iq_result(IQ, #disco_items{}); xmpp:make_iq_result(IQ, #disco_items{});
process_iq(#iq{type = get, lang = Lang, from = From, process_iq(#iq{type = get, sub_els = [#upload_request{filename = File,
sub_els = [#upload_request{filename = File, size = Size,
size = Size, 'content-type' = CType,
'content-type' = CType, xmlns = XMLNS}]} = IQ,
xmlns = XMLNS}]} = IQ, State) ->
#state{server_host = ServerHost, access = Access} = State) -> process_slot_request(IQ, File, Size, CType, XMLNS, State);
process_iq(#iq{type = get, sub_els = [#upload_request_0{filename = File,
size = Size,
'content-type' = CType,
xmlns = XMLNS}]} = IQ,
State) ->
process_slot_request(IQ, File, Size, CType, XMLNS, State);
process_iq(#iq{type = T, lang = Lang} = IQ, _State) when T == get; T == set ->
Txt = <<"No module is handling this query">>,
xmpp:make_error(IQ, xmpp:err_service_unavailable(Txt, Lang));
process_iq(#iq{}, _State) ->
not_request.
-spec process_slot_request(iq(), binary(), pos_integer(), binary(), binary(),
state()) -> {iq(), state()} | iq().
process_slot_request(#iq{lang = Lang, from = From} = IQ,
File, Size, CType, XMLNS,
#state{server_host = ServerHost,
access = Access} = State) ->
case acl:match_rule(ServerHost, Access, From) of case acl:match_rule(ServerHost, Access, From) of
allow -> allow ->
ContentType = yield_content_type(CType), ContentType = yield_content_type(CType),
@ -540,12 +559,7 @@ process_iq(#iq{type = get, lang = Lang, from = From,
[jid:encode(From)]), [jid:encode(From)]),
Txt = <<"Denied by ACL">>, Txt = <<"Denied by ACL">>,
xmpp:make_error(IQ, xmpp:err_forbidden(Txt, Lang)) xmpp:make_error(IQ, xmpp:err_forbidden(Txt, Lang))
end; end.
process_iq(#iq{type = T, lang = Lang} = IQ, _State) when T == get; T == set ->
Txt = <<"No module is handling this query">>,
xmpp:make_error(IQ, xmpp:err_service_unavailable(Txt, Lang));
process_iq(#iq{}, _State) ->
not_request.
-spec create_slot(state(), jid(), binary(), pos_integer(), binary(), binary()) -spec create_slot(state(), jid(), binary(), pos_integer(), binary(), binary())
-> {ok, slot()} | {ok, binary(), binary()} | {error, xmlel()}. -> {ok, slot()} | {ok, binary(), binary()} | {error, xmlel()}.
@ -649,6 +663,8 @@ mk_slot(Slot, #state{put_url = PutPrefix, get_url = GetPrefix}, XMLNS) ->
PutURL = str:join([PutPrefix | Slot], <<$/>>), PutURL = str:join([PutPrefix | Slot], <<$/>>),
GetURL = str:join([GetPrefix | Slot], <<$/>>), GetURL = str:join([GetPrefix | Slot], <<$/>>),
mk_slot(PutURL, GetURL, XMLNS); mk_slot(PutURL, GetURL, XMLNS);
mk_slot(PutURL, GetURL, ?NS_HTTP_UPLOAD_0) ->
#upload_slot_0{get = GetURL, put = PutURL, xmlns = ?NS_HTTP_UPLOAD_0};
mk_slot(PutURL, GetURL, XMLNS) -> mk_slot(PutURL, GetURL, XMLNS) ->
#upload_slot{get = GetURL, put = PutURL, xmlns = XMLNS}. #upload_slot{get = GetURL, put = PutURL, xmlns = XMLNS}.
@ -702,18 +718,27 @@ iq_disco_info(Host, Lang, Name, AddInfo) ->
AddInfo; AddInfo;
MaxSize -> MaxSize ->
MaxSizeStr = integer_to_binary(MaxSize), MaxSizeStr = integer_to_binary(MaxSize),
Fields = [#xdata_field{type = hidden, XData = lists:map(
var = <<"FORM_TYPE">>, fun(NS) ->
values = [?NS_HTTP_UPLOAD]}, Fields = [#xdata_field{
#xdata_field{var = <<"max-file-size">>, type = hidden,
values = [MaxSizeStr]}], var = <<"FORM_TYPE">>,
[#xdata{type = result, fields = Fields}|AddInfo] values = [NS]},
#xdata_field{
var = <<"max-file-size">>,
values = [MaxSizeStr]}],
#xdata{type = result, fields = Fields}
end, [?NS_HTTP_UPLOAD, ?NS_HTTP_UPLOAD_0]),
XData ++ AddInfo
end, end,
#disco_info{identities = [#identity{category = <<"store">>, #disco_info{identities = [#identity{category = <<"store">>,
type = <<"file">>, type = <<"file">>,
name = translate:translate(Lang, Name)}], name = translate:translate(Lang, Name)}],
features = [?NS_HTTP_UPLOAD, ?NS_HTTP_UPLOAD_OLD, features = [?NS_HTTP_UPLOAD,
?NS_DISCO_INFO, ?NS_DISCO_ITEMS], ?NS_HTTP_UPLOAD_0,
?NS_HTTP_UPLOAD_OLD,
?NS_DISCO_INFO,
?NS_DISCO_ITEMS],
xdata = Form}. xdata = Form}.
%% HTTP request handling. %% HTTP request handling.