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"}}},
{stringprep, ".*", {git, "https://github.com/processone/stringprep", {tag, "1.0.8"}}},
{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"}}},
{esip, ".*", {git, "https://github.com/processone/esip", {tag, "1.0.11"}}},
{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));
process_iq(#iq{type = get, sub_els = [#disco_items{}]} = IQ, _State) ->
xmpp:make_iq_result(IQ, #disco_items{});
process_iq(#iq{type = get, lang = Lang, from = From,
sub_els = [#upload_request{filename = File,
size = Size,
'content-type' = CType,
xmlns = XMLNS}]} = IQ,
#state{server_host = ServerHost, access = Access} = State) ->
process_iq(#iq{type = get, sub_els = [#upload_request{filename = File,
size = Size,
'content-type' = CType,
xmlns = XMLNS}]} = IQ,
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
allow ->
ContentType = yield_content_type(CType),
@ -540,12 +559,7 @@ process_iq(#iq{type = get, lang = Lang, from = From,
[jid:encode(From)]),
Txt = <<"Denied by ACL">>,
xmpp:make_error(IQ, xmpp:err_forbidden(Txt, Lang))
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.
end.
-spec create_slot(state(), jid(), binary(), pos_integer(), binary(), binary())
-> {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], <<$/>>),
GetURL = str:join([GetPrefix | Slot], <<$/>>),
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) ->
#upload_slot{get = GetURL, put = PutURL, xmlns = XMLNS}.
@ -702,18 +718,27 @@ iq_disco_info(Host, Lang, Name, AddInfo) ->
AddInfo;
MaxSize ->
MaxSizeStr = integer_to_binary(MaxSize),
Fields = [#xdata_field{type = hidden,
var = <<"FORM_TYPE">>,
values = [?NS_HTTP_UPLOAD]},
#xdata_field{var = <<"max-file-size">>,
values = [MaxSizeStr]}],
[#xdata{type = result, fields = Fields}|AddInfo]
XData = lists:map(
fun(NS) ->
Fields = [#xdata_field{
type = hidden,
var = <<"FORM_TYPE">>,
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,
#disco_info{identities = [#identity{category = <<"store">>,
type = <<"file">>,
name = translate:translate(Lang, Name)}],
features = [?NS_HTTP_UPLOAD, ?NS_HTTP_UPLOAD_OLD,
?NS_DISCO_INFO, ?NS_DISCO_ITEMS],
features = [?NS_HTTP_UPLOAD,
?NS_HTTP_UPLOAD_0,
?NS_HTTP_UPLOAD_OLD,
?NS_DISCO_INFO,
?NS_DISCO_ITEMS],
xdata = Form}.
%% HTTP request handling.