<commit_before>% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

-module(couch_rep_httpc).
-include("couch_db.hrl").
-include("../ibrowse/ibrowse.hrl").

-export([db_exists/1, full_url/1, request/1, spawn_worker_process/1,
    spawn_link_worker_process/1]).

request(Req) when is_record(Req, http_db) ->
    do_request(Req).

do_request(#http_db{url=Url} = Req) when is_binary(Url) ->
    do_request(Req#http_db{url = ?b2l(Url)});

do_request(Req) ->
    #http_db{
        auth = Auth,
        headers = Headers0,
        method = Method,
        body = B,
        options = Opts,
        conn = Conn
    } = Req,
    Url = full_url(Req),
    Headers = case proplists:get_value(<<"oauth">>, Auth) of
    undefined ->
        Headers0;
    {OAuthProps} ->
        [oauth_header(Url, Method, OAuthProps) | Headers0]
    end,
    Body = case B of
    {Fun, InitialState} when is_function(Fun) ->
        {Fun, InitialState};
    nil ->
        [];
    _Else ->
        iolist_to_binary(?JSON_ENCODE(B)) 
    end,
    Resp = case Conn of
    nil ->
        ibrowse:send_req(Url, Headers, Method, Body, Opts, infinity);
    _ ->
        ibrowse:send_req_direct(Conn, Url, Headers, Method, Body, Opts, infinity)
    end,
    process_response(Resp, Req).

db_exists(Req) ->
    db_exists(Req, Req#http_db.url).

db_exists(Req, CanonicalUrl) ->
    #http_db{
        url = Url,
        headers = Headers
    } = Req,
    case catch ibrowse:send_req(Url, Headers, head) of
    {ok, "200", _, _} ->
        Req#http_db{url = CanonicalUrl};
    {ok, "301", RespHeaders, _} ->
        MochiHeaders = mochiweb_headers:make(RespHeaders),<commit_msg>fix replication from DB protected by OAuth and require_valid_user=true<commit_after>% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

-module(couch_rep_httpc).
-include("couch_db.hrl").
-include("../ibrowse/ibrowse.hrl").

-export([db_exists/1, full_url/1, request/1, spawn_worker_process/1,
    spawn_link_worker_process/1]).

request(Req) when is_record(Req, http_db) ->
    do_request(Req).

do_request(#http_db{url=Url} = Req) when is_binary(Url) ->
    do_request(Req#http_db{url = ?b2l(Url)});

do_request(Req) ->
    #http_db{
        auth = Auth,
        headers = Headers0,
        method = Method,
        body = B,
        options = Opts,
        conn = Conn
    } = Req,
    Url = full_url(Req),
    Headers = case proplists:get_value(<<"oauth">>, Auth) of
    undefined ->
        Headers0;
    {OAuthProps} ->
        [oauth_header(Url, Method, OAuthProps) | Headers0]
    end,
    Body = case B of
    {Fun, InitialState} when is_function(Fun) ->
        {Fun, InitialState};
    nil ->
        [];
    _Else ->
        iolist_to_binary(?JSON_ENCODE(B)) 
    end,
    Resp = case Conn of
    nil ->
        ibrowse:send_req(Url, Headers, Method, Body, Opts, infinity);
    _ ->
        ibrowse:send_req_direct(Conn, Url, Headers, Method, Body, Opts, infinity)
    end,
    process_response(Resp, Req).

db_exists(Req) ->
    db_exists(Req, Req#http_db.url).

db_exists(Req, CanonicalUrl) ->
    #http_db{
        auth = Auth,
        headers = Headers0,
        url = Url
    } = Req,
    Headers = case proplists:get_value(<<"oauth">>, Auth) of
    undefined ->
        Headers0;
    {OAuthProps} ->
        [oauth_header(Url, get, OAuthProps) | Headers0]
    end,
    case catch ibrowse:send_req(Url, Headers, head) of
    {ok, "200", _, _} ->
        Req#http_db{url = CanonicalUrl};
    {ok, "301", RespHeaders, _} ->
        MochiHeaders = mochiweb_headers:make(RespHeaders),
{'fraction_non_alphanumeric': 0.1237821080602303, 'fraction_numerical': 0.00907883082373782, 'mean_word_length': 2.9449781659388647, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4800494', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1526, 'n_words': 400}