Skip to content
This repository was archived by the owner on May 25, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/couch_replicator_js_functions.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
throw({forbidden: error_msg});
}

function validateUrl(endpoint, fieldName) {
if ((endpoint.substr(0, 7) !== 'http://') &&
endpoint.substr(0, 8) !== 'https://') {
reportError('The `' + fieldName + '\\' property must be an HTTP' +
' or HTTPS URL');
}
}

function validateEndpoint(endpoint, fieldName) {
if ((typeof endpoint !== 'string') &&
((typeof endpoint !== 'object') || (endpoint === null))) {
Expand All @@ -26,12 +34,20 @@
' and be either a string or an object.');
}

if (typeof endpoint === 'string') {
validateUrl(endpoint, fieldName);
}

if (typeof endpoint === 'object') {
if ((typeof endpoint.url !== 'string') || !endpoint.url) {
reportError('The url property must exist in the `' +
fieldName + '\\' field and must be a non-empty string.');
}

if (typeof endpoint.url === 'string') {
validateUrl(endpoint.url, fieldName);
}

if ((typeof endpoint.auth !== 'undefined') &&
((typeof endpoint.auth !== 'object') ||
endpoint.auth === null)) {
Expand Down
49 changes: 33 additions & 16 deletions src/couch_replicator_manager.erl
Original file line number Diff line number Diff line change
Expand Up @@ -795,23 +795,40 @@ ensure_rep_ddoc_exists(RepDb) ->

ensure_rep_ddoc_exists(RepDb, DDocId) ->
case open_rep_doc(RepDb, DDocId) of
{ok, _Doc} ->
ok;
_ ->
DDoc = couch_doc:from_json_obj({[
{<<"_id">>, DDocId},
{<<"language">>, <<"javascript">>},
{<<"validate_doc_update">>, ?REP_DB_DOC_VALIDATE_FUN}
]}),
try
{ok, _} = save_rep_doc(RepDb, DDoc)
catch
throw:conflict ->
% NFC what to do about this other than
% not kill the process.
ok
{not_found, _Reason} ->
{ok, DDoc} = replication_design_doc(DDocId),
couch_log:notice("creating replicator ddoc", []),
{ok, _Rev} = save_rep_doc(RepDb, DDoc);
{ok, Doc} ->
{Props} = couch_doc:to_json_obj(Doc, []),
case couch_util:get_value(<<"validate_doc_update">>, Props, []) of
?REP_DB_DOC_VALIDATE_FUN ->
ok;
_ ->
Props1 = lists:keyreplace(<<"validate_doc_update">>, 1, Props,
{<<"validate_doc_update">>,
?REP_DB_DOC_VALIDATE_FUN}),
DDoc = couch_doc:from_json_obj({Props1}),
couch_log:notice("updating replicator ddoc", []),
try
{ok, _} = save_rep_doc(RepDb, DDoc)
catch
throw:conflict ->
%% ignore, we'll retry next time
ok
end
end
end.
end,
ok.

replication_design_doc(DDocId) ->
DocProps = [
{<<"_id">>, DDocId},
{<<"language">>, <<"javascript">>},
{<<"validate_doc_update">>, ?REP_DB_DOC_VALIDATE_FUN}
],
{ok, couch_doc:from_json_obj({DocProps})}.


% pretty-print replication id
pp_rep_id(#rep{id = RepId}) ->
Expand Down