Arduino DLNA Server
HttpRequest.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <WiFi.h>
4 
5 #include "HttpHeader.h"
6 // #include "Platform/AltClient.h"
7 #include "HttpChunkReader.h"
8 #include "WiFiClient.h"
9 
10 namespace tiny_dlna {
11 
21 class HttpRequest {
22  public:
24  DlnaLogger.log(DlnaInfo, "HttpRequest");
25  // default_client.setInsecure();
27  }
28 
29  HttpRequest(Client &client) {
30  DlnaLogger.log(DlnaInfo, "HttpRequest");
32  }
33 
34  void setClient(Client &client) { this->client_ptr = &client; }
35 
36  // the requests usually need a host. This needs to be set if we did not
37  // provide a URL
38  void setHost(const char *host) {
39  DlnaLogger.log(DlnaInfo, "setHost", host);
40  this->host_name = host;
41  }
42 
43  operator bool() { return client_ptr != nullptr && (bool)*client_ptr; }
44 
45  virtual bool connected() { return client_ptr->connected(); }
46 
47  virtual int available() {
48  if (reply_header.isChunked()) {
49  return chunk_reader.available();
50  }
51  return client_ptr->available();
52  }
53 
54  virtual void stop() {
55  DlnaLogger.log(DlnaInfo, "stop");
56  client_ptr->stop();
57  }
58 
59  virtual int post(Url &url, const char *mime, const char *data, int len = -1) {
60  DlnaLogger.log(DlnaInfo, "post %s", url.url());
61  return process(T_POST, url, mime, data, len);
62  }
63 
64  virtual int put(Url &url, const char *mime, const char *data, int len = -1) {
65  DlnaLogger.log(DlnaInfo, "put %s", url.url());
66  return process(T_PUT, url, mime, data, len);
67  }
68 
69  virtual int del(Url &url, const char *mime = nullptr,
70  const char *data = nullptr, int len = -1) {
71  DlnaLogger.log(DlnaInfo, "del %s", url.url());
72  return process(T_DELETE, url, mime, data, len);
73  }
74 
75  virtual int get(Url &url, const char *acceptMime = nullptr,
76  const char *data = nullptr, int len = -1) {
77  DlnaLogger.log(DlnaInfo, "get %s", str(url.url()));
78  this->accept = acceptMime;
79  return process(T_GET, url, nullptr, data, len);
80  }
81 
82  virtual int head(Url &url, const char *acceptMime = nullptr,
83  const char *data = nullptr, int len = -1) {
84  DlnaLogger.log(DlnaInfo, "head %s", url.url());
85  this->accept = acceptMime;
86  return process(T_HEAD, url, nullptr, data, len);
87  }
88 
89  // reads the reply data
90  virtual int read(uint8_t *str, int len) {
91  if (reply_header.isChunked()) {
92  return chunk_reader.read(*client_ptr, str, len);
93  } else {
94  return client_ptr->read(str, len);
95  }
96  }
97 
98  // read the reply data up to the next new line. For Chunked data we provide
99  // the full chunk!
100  virtual int readln(uint8_t *str, int len, bool incl_nl = true) {
101  if (reply_header.isChunked()) {
102  return chunk_reader.readln(*client_ptr, str, len);
103  } else {
104  return chunk_reader.readlnInternal(*client_ptr, str, len, incl_nl);
105  }
106  }
107 
108  // provides the head information of the reply
109  virtual HttpReplyHeader &reply() { return reply_header; }
110 
111  virtual HttpRequestHeader &request() { return request_header; }
112 
113  virtual void setAgent(const char *agent) { this->agent = agent; }
114 
115  virtual void setConnection(const char *connection) {
116  this->connection = connection;
117  }
118 
119  virtual void setAcceptsEncoding(const char *enc) {
120  this->accept_encoding = enc;
121  }
122 
123  Client *client() { return client_ptr; }
124 
125  protected:
126  WiFiClient default_client;
127  Client *client_ptr;
133  const char *agent = nullptr;
134  const char *connection = CON_CLOSE;
135  const char *accept = ACCEPT_ALL;
136  const char *accept_encoding = nullptr;
137 
138  const char *str(const char *in) { return in == nullptr ? "" : in; }
139 
140  // opens a connection to the indicated host
141  virtual int connect(const char *ip, uint16_t port) {
142  DlnaLogger.log(DlnaInfo, "connect %s", ip);
143  return this->client_ptr->connect(ip, port);
144  }
145 
146  // sends request and reads the reply_header from the server
147  virtual int process(TinyMethodID action, Url &url, const char *mime,
148  const char *data, int len = -1) {
149  if (!connected()) {
150  char msg[1024];
151  sprintf(msg, "connecting to host %s port %d", url.host(), url.port());
152  DlnaLogger.log(DlnaInfo, "process %s", msg);
153 
154  connect(url.host(), url.port());
155  if (host_name.isEmpty()) {
156  host_name = url.host();
157  host_name.add(":");
158  host_name.add(url.port());
159  }
160  }
161  request_header.setValues(action, url.path());
162  if (len == -1 && data != nullptr) {
163  len = strlen(data);
164  }
165  if (len > 0) {
167  }
168  if (!host_name.isEmpty()) {
170  }
171  if (agent != nullptr) {
173  }
174  if (accept_encoding != nullptr) {
176  }
177  if (mime != nullptr) {
179  }
180 
183 
185 
186  if (len > 0) {
187  DlnaLogger.log(DlnaInfo, "process - writing data");
188  client_ptr->write((const uint8_t *)data, len);
189  }
190 
192 
193  // if we use chunked tranfer we need to read the first chunked length
194  if (reply_header.isChunked()) {
196  };
197 
198  return reply_header.statusCode();
199  }
200 };
201 
202 } // namespace tiny_dlna
Http might reply with chunks. So we need to dechunk the data. see https://en.wikipedia....
Definition: HttpChunkReader.h:13
void open(Client &client)
Definition: HttpChunkReader.h:28
virtual int read(Client &client, uint8_t *str, int len)
Definition: HttpChunkReader.h:35
virtual int readln(Client &client, uint8_t *str, int len, bool incl_nl=true)
Definition: HttpChunkReader.h:55
int available()
Definition: HttpChunkReader.h:73
void read(Client &in)
Definition: HttpHeader.h:234
int statusCode()
Definition: HttpHeader.h:224
HttpHeader & put(const char *key, const char *value)
Definition: HttpHeader.h:102
bool isChunked()
Definition: HttpHeader.h:228
void write(Client &out)
Definition: HttpHeader.h:266
virtual int readlnInternal(Stream &client, uint8_t *str, int len, bool incl_nl=true)
Definition: HttpLineReader.h:18
Reading and Writing of Http Replys.
Definition: HttpHeader.h:411
Reading and writing of Http Requests.
Definition: HttpHeader.h:357
HttpHeader & setValues(TinyMethodID id, const char *urlPath, const char *protocol=nullptr)
Definition: HttpHeader.h:360
Simple API to process get, put, post, del http requests I tried to use Arduino HttpClient,...
Definition: HttpRequest.h:21
virtual int available()
Definition: HttpRequest.h:47
Url url
Definition: HttpRequest.h:128
const char * agent
Definition: HttpRequest.h:133
virtual HttpRequestHeader & request()
Definition: HttpRequest.h:111
virtual HttpReplyHeader & reply()
Definition: HttpRequest.h:109
HttpReplyHeader reply_header
Definition: HttpRequest.h:130
const char * accept
Definition: HttpRequest.h:135
HttpRequestHeader request_header
Definition: HttpRequest.h:129
Str host_name
Definition: HttpRequest.h:132
virtual int put(Url &url, const char *mime, const char *data, int len=-1)
Definition: HttpRequest.h:64
virtual int get(Url &url, const char *acceptMime=nullptr, const char *data=nullptr, int len=-1)
Definition: HttpRequest.h:75
virtual void setAcceptsEncoding(const char *enc)
Definition: HttpRequest.h:119
virtual void setAgent(const char *agent)
Definition: HttpRequest.h:113
HttpRequest()
Definition: HttpRequest.h:23
const char * accept_encoding
Definition: HttpRequest.h:136
virtual bool connected()
Definition: HttpRequest.h:45
virtual void setConnection(const char *connection)
Definition: HttpRequest.h:115
Client * client()
Definition: HttpRequest.h:123
void setHost(const char *host)
Definition: HttpRequest.h:38
virtual int head(Url &url, const char *acceptMime=nullptr, const char *data=nullptr, int len=-1)
Definition: HttpRequest.h:82
HttpRequest(Client &client)
Definition: HttpRequest.h:29
const char * str(const char *in)
Definition: HttpRequest.h:138
virtual int post(Url &url, const char *mime, const char *data, int len=-1)
Definition: HttpRequest.h:59
virtual int readln(uint8_t *str, int len, bool incl_nl=true)
Definition: HttpRequest.h:100
Client * client_ptr
Definition: HttpRequest.h:127
const char * connection
Definition: HttpRequest.h:134
virtual int process(TinyMethodID action, Url &url, const char *mime, const char *data, int len=-1)
Definition: HttpRequest.h:147
virtual int del(Url &url, const char *mime=nullptr, const char *data=nullptr, int len=-1)
Definition: HttpRequest.h:69
virtual int connect(const char *ip, uint16_t port)
Definition: HttpRequest.h:141
WiFiClient default_client
Definition: HttpRequest.h:126
virtual void stop()
Definition: HttpRequest.h:54
HttpChunkReader chunk_reader
Definition: HttpRequest.h:131
virtual int read(uint8_t *str, int len)
Definition: HttpRequest.h:90
void setClient(Client &client)
Definition: HttpRequest.h:34
void log(DlnaLogLevel current_level, const char *fmt...)
Print log message.
Definition: Logger.h:40
virtual void add(int value)
adds a int value
Definition: StrView.h:130
virtual bool isEmpty()
checks if the string is empty
Definition: StrView.h:373
String implementation which keeps the data on the heap. We grow the allocated memory only if the copy...
Definition: Str.h:22
const char * c_str()
provides the string value as const char*
Definition: Str.h:188
URL parser which breaks a full url string up into its individual parts.
Definition: Url.h:18
const char * path()
Definition: Url.h:47
int port()
Definition: Url.h:51
const char * url()
Definition: Url.h:46
const char * host()
Definition: Url.h:48
Definition: Allocator.h:6
const char * CONTENT_TYPE
Definition: HttpHeader.h:16
@ DlnaInfo
Definition: Logger.h:16
const char * CONTENT_LENGTH
Definition: HttpHeader.h:17
const char * CON_CLOSE
Definition: HttpHeader.h:19
const char * HOST_C
Definition: HttpHeader.h:29
TinyMethodID
Definition: HttpHeader.h:35
@ T_DELETE
Definition: HttpHeader.h:41
@ T_HEAD
Definition: HttpHeader.h:38
@ T_GET
Definition: HttpHeader.h:37
@ T_PUT
Definition: HttpHeader.h:40
@ T_POST
Definition: HttpHeader.h:39
const char * ACCEPT
Definition: HttpHeader.h:23
const char * ACCEPT_ENCODING
Definition: HttpHeader.h:30
LoggerClass DlnaLogger
Definition: Logger.cpp:5
const char * ACCEPT_ALL
Definition: HttpHeader.h:24
const char * USER_AGENT
Definition: HttpHeader.h:26
const char * CONNECTION
Definition: HttpHeader.h:18