Shaka Player Embedded
xml_http_request.h
Go to the documentation of this file.
1 // Copyright 2016 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SHAKA_EMBEDDED_JS_XML_HTTP_REQUEST_H_
16 #define SHAKA_EMBEDDED_JS_XML_HTTP_REQUEST_H_
17 
18 #include <curl/curl.h>
19 
20 #include <atomic>
21 #include <map>
22 #include <string>
23 
24 #include "shaka/optional.h"
25 #include "shaka/variant.h"
26 #include "src/debug/mutex.h"
31 #include "src/mapping/enum.h"
34 
35 namespace shaka {
36 class NetworkThread;
37 
38 namespace js {
39 
59  DECLARE_TYPE_INFO(XMLHttpRequest);
60 
61  public:
62  enum class ReadyState {
63  Unsent = 0,
64  Opened = 1,
65  HeadersReceived = 2,
66  Loading = 3,
67  Done = 4,
68  };
69 
71  static XMLHttpRequest* Create() {
72  return new XMLHttpRequest();
73  }
74 
75  void Trace(memory::HeapTracer* tracer) const override;
76  bool IsShortLived() const override;
77 
78  void Abort();
79  std::string GetAllResponseHeaders() const;
80  optional<std::string> GetResponseHeader(const std::string& name) const;
81  ExceptionOr<void> Open(const std::string& method, const std::string& url,
83  optional<std::string> password);
85  ExceptionOr<void> SetRequestHeader(const std::string& key,
86  const std::string& value);
87  bool WithCredentials() const;
88  ExceptionOr<void> SetWithCredentials(bool with_credentials);
89 
93  void OnDataReceived(uint8_t* buffer, size_t length);
94 
100  void OnHeaderReceived(const uint8_t* buffer, size_t length);
101 
103  size_t OnUpload(uint8_t* buffer, size_t length);
104 
113 
116  std::string response_text;
117  std::string response_type;
118  std::string response_url;
119  int status;
120  std::string status_text;
121  uint64_t timeout_ms; // JavaScript "timeout"
122 
123  private:
124  friend NetworkThread;
125 
126  void RaiseProgressEvents();
127 
129  void OnRequestComplete(CURLcode code);
130 
131  void Reset();
132 
133  mutable Mutex mutex_;
134  std::map<std::string, std::string> response_headers_;
135  util::DynamicBuffer temp_data_;
136  ByteBuffer upload_data_;
137 
138  CURL* curl_;
139  curl_slist* request_headers_;
140  size_t upload_pos_;
141  uint64_t last_progress_time_;
142  double estimated_size_;
143  bool parsing_headers_;
144  bool with_credentials_;
145  std::atomic<bool> abort_pending_;
146 };
147 
149  : public BackingObjectFactory<XMLHttpRequest, events::EventTarget> {
150  public:
152 };
153 
154 } // namespace js
155 } // namespace shaka
156 
158 
159 #endif // SHAKA_EMBEDDED_JS_XML_HTTP_REQUEST_H_
std::string GetAllResponseHeaders() const
static XMLHttpRequest * Create()
void Trace(memory::HeapTracer *tracer) const override
optional< std::string > GetResponseHeader(const std::string &name) const
CONVERT_ENUM_AS_NUMBER(shaka::js, XMLHttpRequest::ReadyState)
Definition: data.h:29
void OnHeaderReceived(const uint8_t *buffer, size_t length)
ExceptionOr< void > Open(const std::string &method, const std::string &url, optional< bool > async, optional< std::string > user, optional< std::string > password)
bool IsShortLived() const override
ExceptionOr< void > SetWithCredentials(bool with_credentials)
ExceptionOr< void > Send(optional< variant< ByteBuffer, ByteString >> maybe_data)
void OnDataReceived(uint8_t *buffer, size_t length)
size_t OnUpload(uint8_t *buffer, size_t length)
std::string name() const
ExceptionOr< void > SetRequestHeader(const std::string &key, const std::string &value)