<commit_before>// Copyright (c) 2017 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "atom/browser/net/url_request_stream_job.h"

#include <algorithm>
#include <memory>
#include <ostream>
#include <string>
#include <utility>

#include "atom/common/api/event_emitter_caller.h"
#include "atom/common/atom_constants.h"
#include "atom/common/native_mate_converters/net_converter.h"
#include "atom/common/node_includes.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "native_mate/dictionary.h"
#include "net/base/net_errors.h"
#include "net/filter/gzip_source_stream.h"

namespace atom {

namespace {

void BeforeStartInUI(base::WeakPtr<URLRequestStreamJob> job,
                     mate::Arguments* args) {
  v8::Local<v8::Value> value;
  int error = net::OK;
  bool ended = false;
  if (!args->GetNext(&value) || !value->IsObject()) {
    // Invalid opts.
    content::BrowserThread::PostTask(
        content::BrowserThread::IO, FROM_HERE,
        base::BindOnce(&URLRequestStreamJob::OnError, job, net::ERR_FAILED));
    return;
  }

  mate::Dictionary opts(args->isolate(), v8::Local<v8::Object>::Cast(value));
  int status_code;
  if (!opts.Get("statusCode", &status_code)) {
    // assume HTTP OK if statusCode is not passed.
    status_code = 200;
  }
  std::string status("HTTP/1.1 ");
  status.append(base::IntToString(status_code));
  status.append(" ");
  status.append(
      net::GetHttpReasonPhrase(static_cast<net::HttpStatusCode>(status_code)));
  status.append("\0\0", 2);
  scoped_refptr<net::HttpResponseHeaders> response_headers(
      new net::HttpResponseHeaders(status));

  if (opts.Get("headers", &value)) {
    mate::Converter<net::HttpResponseHeaders*>::FromV8(args->isolate(), value,
                                                       response_headers.get());
  }

  if (!opts.Get("data", &value)) {
    // Assume the opts is already a stream
    value = opts.GetHandle();
  } else if (value->IsNullOrUndefined()) {
    // "data" was explicitly passed as null or undefined, assume the user wants
    // to send an empty body.
    ended = true;
    content::BrowserThread::PostTask(
        content::BrowserThread::IO, FROM_HERE,
        base::BindOnce(&URLRequestStreamJob::StartAsync, job, nullptr,
                       base::RetainedRef(response_headers), ended, error));
    return;
  }

  mate::Dictionary data(args->isolate(), v8::Local<v8::Object>::Cast(value));
  if (!data.Get("on", &value) || !value->IsFunction() ||
      !data.Get("removeListener", &value) || !value->IsFunction()) {
    // If data is passed but it is not a stream, signal an error.
    content::BrowserThread::PostTask(
        content::BrowserThread::IO, FROM_HERE,
        base::BindOnce(&URLRequestStreamJob::OnError, job, net::ERR_FAILED));
    return;
  }

  auto subscriber = std::make_unique<mate::StreamSubscriber>(
      args->isolate(), data.GetHandle(), job);

  content::BrowserThread::PostTask(
      content::BrowserThread::IO, FROM_HERE,
      base::BindOnce(&URLRequestStreamJob::StartAsync, job,
                     std::move(subscriber), base::RetainedRef(response_headers),
                     ended, error));
}

}  // namespace

URLRequestStreamJob::URLRequestStreamJob(net::URLRequest* request,
                                         net::NetworkDelegate* network_delegate)
    : net::URLRequestJob(request, network_delegate),
      pending_buf_(nullptr),
      pending_buf_size_(0),
      ended_(false),
      response_headers_(nullptr),
      weak_factory_(this) {}

URLRequestStreamJob::~URLRequestStreamJob() {
  if (subscriber_) {
    content::BrowserThread::DeleteSoon(content::BrowserThread::UI, FROM_HERE,
                                       std::move(subscriber_));
  }
}

void URLRequestStreamJob::Start() {
  auto request_details = std::make_unique<base::DictionaryValue>();
  FillRequestDetails(request_details.get(), request());
  content::BrowserThread::PostTask(
      content::BrowserThread::UI, FROM_HERE,
      base::BindOnce(&JsAsker::AskForOptions, base::Unretained(isolate()),
                     handler(), std::move(request_details),
                     base::Bind(&BeforeStartInUI, weak_factory_.GetWeakPtr())));
}

void URLRequestStreamJob::StartAsync(
    std::unique_ptr<mate::StreamSubscriber> subscriber,
    scoped_refptr<net::HttpResponseHeaders> response_headers,
    bool ended,
    int error) {
  if (error != net::OK) {
    NotifyStartError(
        net::URLRequestStatus(net::URLRequestStatus::FAILED, error));
    return;
  }

  ended_ = ended;
  response_headers_ = response_headers;
  subscriber_ = std::move(subscriber);
  request_start_time_ = base::TimeTicks::Now();
  NotifyHeadersComplete();
}

void URLRequestStreamJob::OnData(std::vector<char>&& buffer) {  // NOLINT
  if (write_buffer_.empty()) {
    // Quick branch without copying.
    write_buffer_ = std::move(buffer);
  } else {
    // write_buffer_ += buffer
    size_t len = write_buffer_.size();
    write_buffer_.resize(len + buffer.size());
    std::copy(buffer.begin(), buffer.end(), write_buffer_.begin() + len);
  }

  // Copy to output.
  if (pending_buf_) {
    int len = BufferCopy(&write_buffer_, pending_buf_.get(), pending_buf_size_);
    write_buffer_.erase(write_buffer_.begin(), write_buffer_.begin() + len);
    ReadRawDataComplete(len);
  }
}

void URLRequestStreamJob::OnEnd() {
  ended_ = true;
  ReadRawDataComplete(0);
}

void URLRequestStreamJob::OnError(int error) {
  NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, error));
}

int URLRequestStreamJob::ReadRawData(net::IOBuffer* dest, int dest_size) {
  response_start_time_ = base::TimeTicks::Now();
<commit_msg>fix: don't call ReadRawDataComplete if it's not necessary (#15185)<commit_after>// Copyright (c) 2017 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "atom/browser/net/url_request_stream_job.h"

#include <algorithm>
#include <memory>
#include <ostream>
#include <string>
#include <utility>

#include "atom/common/api/event_emitter_caller.h"
#include "atom/common/atom_constants.h"
#include "atom/common/native_mate_converters/net_converter.h"
#include "atom/common/node_includes.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "native_mate/dictionary.h"
#include "net/base/net_errors.h"
#include "net/filter/gzip_source_stream.h"

namespace atom {

namespace {

void BeforeStartInUI(base::WeakPtr<URLRequestStreamJob> job,
                     mate::Arguments* args) {
  v8::Local<v8::Value> value;
  int error = net::OK;
  bool ended = false;
  if (!args->GetNext(&value) || !value->IsObject()) {
    // Invalid opts.
    content::BrowserThread::PostTask(
        content::BrowserThread::IO, FROM_HERE,
        base::BindOnce(&URLRequestStreamJob::OnError, job, net::ERR_FAILED));
    return;
  }

  mate::Dictionary opts(args->isolate(), v8::Local<v8::Object>::Cast(value));
  int status_code;
  if (!opts.Get("statusCode", &status_code)) {
    // assume HTTP OK if statusCode is not passed.
    status_code = 200;
  }
  std::string status("HTTP/1.1 ");
  status.append(base::IntToString(status_code));
  status.append(" ");
  status.append(
      net::GetHttpReasonPhrase(static_cast<net::HttpStatusCode>(status_code)));
  status.append("\0\0", 2);
  scoped_refptr<net::HttpResponseHeaders> response_headers(
      new net::HttpResponseHeaders(status));

  if (opts.Get("headers", &value)) {
    mate::Converter<net::HttpResponseHeaders*>::FromV8(args->isolate(), value,
                                                       response_headers.get());
  }

  if (!opts.Get("data", &value)) {
    // Assume the opts is already a stream
    value = opts.GetHandle();
  } else if (value->IsNullOrUndefined()) {
    // "data" was explicitly passed as null or undefined, assume the user wants
    // to send an empty body.
    ended = true;
    content::BrowserThread::PostTask(
        content::BrowserThread::IO, FROM_HERE,
        base::BindOnce(&URLRequestStreamJob::StartAsync, job, nullptr,
                       base::RetainedRef(response_headers), ended, error));
    return;
  }

  mate::Dictionary data(args->isolate(), v8::Local<v8::Object>::Cast(value));
  if (!data.Get("on", &value) || !value->IsFunction() ||
      !data.Get("removeListener", &value) || !value->IsFunction()) {
    // If data is passed but it is not a stream, signal an error.
    content::BrowserThread::PostTask(
        content::BrowserThread::IO, FROM_HERE,
        base::BindOnce(&URLRequestStreamJob::OnError, job, net::ERR_FAILED));
    return;
  }

  auto subscriber = std::make_unique<mate::StreamSubscriber>(
      args->isolate(), data.GetHandle(), job);

  content::BrowserThread::PostTask(
      content::BrowserThread::IO, FROM_HERE,
      base::BindOnce(&URLRequestStreamJob::StartAsync, job,
                     std::move(subscriber), base::RetainedRef(response_headers),
                     ended, error));
}

}  // namespace

URLRequestStreamJob::URLRequestStreamJob(net::URLRequest* request,
                                         net::NetworkDelegate* network_delegate)
    : net::URLRequestJob(request, network_delegate),
      pending_buf_(nullptr),
      pending_buf_size_(0),
      ended_(false),
      response_headers_(nullptr),
      weak_factory_(this) {}

URLRequestStreamJob::~URLRequestStreamJob() {
  if (subscriber_) {
    content::BrowserThread::DeleteSoon(content::BrowserThread::UI, FROM_HERE,
                                       std::move(subscriber_));
  }
}

void URLRequestStreamJob::Start() {
  auto request_details = std::make_unique<base::DictionaryValue>();
  FillRequestDetails(request_details.get(), request());
  content::BrowserThread::PostTask(
      content::BrowserThread::UI, FROM_HERE,
      base::BindOnce(&JsAsker::AskForOptions, base::Unretained(isolate()),
                     handler(), std::move(request_details),
                     base::Bind(&BeforeStartInUI, weak_factory_.GetWeakPtr())));
}

void URLRequestStreamJob::StartAsync(
    std::unique_ptr<mate::StreamSubscriber> subscriber,
    scoped_refptr<net::HttpResponseHeaders> response_headers,
    bool ended,
    int error) {
  if (error != net::OK) {
    NotifyStartError(
        net::URLRequestStatus(net::URLRequestStatus::FAILED, error));
    return;
  }

  ended_ = ended;
  response_headers_ = response_headers;
  subscriber_ = std::move(subscriber);
  request_start_time_ = base::TimeTicks::Now();
  NotifyHeadersComplete();
}

void URLRequestStreamJob::OnData(std::vector<char>&& buffer) {  // NOLINT
  if (write_buffer_.empty()) {
    // Quick branch without copying.
    write_buffer_ = std::move(buffer);
  } else {
    // write_buffer_ += buffer
    size_t len = write_buffer_.size();
    write_buffer_.resize(len + buffer.size());
    std::copy(buffer.begin(), buffer.end(), write_buffer_.begin() + len);
  }

  // Copy to output.
  if (pending_buf_) {
    int len = BufferCopy(&write_buffer_, pending_buf_.get(), pending_buf_size_);
    write_buffer_.erase(write_buffer_.begin(), write_buffer_.begin() + len);
    pending_buf_ = nullptr;
    pending_buf_size_ = 0;
    ReadRawDataComplete(len);
  }
}

void URLRequestStreamJob::OnEnd() {
  ended_ = true;
  if (pending_buf_) {
    ReadRawDataComplete(0);
  }
}

void URLRequestStreamJob::OnError(int error) {
  NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, error));
}

int URLRequestStreamJob::ReadRawData(net::IOBuffer* dest, int dest_size) {
  response_start_time_ = base::TimeTicks::Now();

{'fraction_non_alphanumeric': 0.14407212110903214, 'fraction_numerical': 0.004082326926348018, 'mean_word_length': 3.980516730199068, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6634960', 'n_tokens_mistral': 3629, 'n_tokens_neox': 3578, 'n_words': 664}