mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-07 16:23:38 +03:00
add Authorization support for HTTP client
This commit is contained in:
parent
62f38bfef3
commit
a9ce1b4f2e
@ -26,6 +26,7 @@
|
|||||||
|
|
||||||
|
|
||||||
#define CORE_HAS_LIBB64
|
#define CORE_HAS_LIBB64
|
||||||
|
#define CORE_HAS_BASE64_CLASS
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -26,9 +26,11 @@
|
|||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <WiFiClientSecure.h>
|
#include <WiFiClientSecure.h>
|
||||||
#include <StreamString.h>
|
#include <StreamString.h>
|
||||||
|
#include <base64.h>
|
||||||
|
|
||||||
#include "ESP8266HTTPClient.h"
|
#include "ESP8266HTTPClient.h"
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* constractor
|
* constractor
|
||||||
*/
|
*/
|
||||||
@ -219,6 +221,20 @@ void HTTPClient::setUserAgent(const char * userAgent) {
|
|||||||
_userAgent = userAgent;
|
_userAgent = userAgent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set the Authorizatio for the http request
|
||||||
|
* @param user const char *
|
||||||
|
* @param password const char *
|
||||||
|
*/
|
||||||
|
void HTTPClient::setAuthorization(const char * user, const char * password) {
|
||||||
|
if(user && password) {
|
||||||
|
String auth = user;
|
||||||
|
auth += ":";
|
||||||
|
auth += password;
|
||||||
|
_base64Authorization = base64::encode(auth);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* send a GET request
|
* send a GET request
|
||||||
* @return http code
|
* @return http code
|
||||||
@ -490,7 +506,7 @@ String HTTPClient::errorToString(int error) {
|
|||||||
void HTTPClient::addHeader(const String& name, const String& value, bool first) {
|
void HTTPClient::addHeader(const String& name, const String& value, bool first) {
|
||||||
|
|
||||||
// not allow set of Header handled by code
|
// not allow set of Header handled by code
|
||||||
if(!name.equalsIgnoreCase("Connection") && !name.equalsIgnoreCase("User-Agent") && !name.equalsIgnoreCase("Host")) {
|
if(!name.equalsIgnoreCase("Connection") && !name.equalsIgnoreCase("User-Agent") && !name.equalsIgnoreCase("Host") && !(_base64Authorization.length() && name.equalsIgnoreCase("Authorization"))) {
|
||||||
String headerLine = name;
|
String headerLine = name;
|
||||||
headerLine += ": ";
|
headerLine += ": ";
|
||||||
headerLine += value;
|
headerLine += value;
|
||||||
@ -622,7 +638,13 @@ bool HTTPClient::sendHeader(const char * type) {
|
|||||||
} else {
|
} else {
|
||||||
header += "close";
|
header += "close";
|
||||||
}
|
}
|
||||||
header += "\r\n" + _Headers + "\r\n";
|
header += "\r\n";
|
||||||
|
|
||||||
|
if(_base64Authorization.length()) {
|
||||||
|
header += "Authorization: Basic " + _base64Authorization + "\r\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
header += _Headers + "\r\n";
|
||||||
|
|
||||||
return (_tcp->write(header.c_str(), header.length()) == header.length());
|
return (_tcp->write(header.c_str(), header.length()) == header.length());
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@
|
|||||||
#ifndef ESP8266HTTPClient_H_
|
#ifndef ESP8266HTTPClient_H_
|
||||||
#define ESP8266HTTPClient_H_
|
#define ESP8266HTTPClient_H_
|
||||||
|
|
||||||
//#define DEBUG_HTTPCLIENT(...) Serial1.printf( __VA_ARGS__ )
|
#define DEBUG_HTTPCLIENT(...) Serial1.printf( __VA_ARGS__ )
|
||||||
|
|
||||||
#ifndef DEBUG_HTTPCLIENT
|
#ifndef DEBUG_HTTPCLIENT
|
||||||
#define DEBUG_HTTPCLIENT(...)
|
#define DEBUG_HTTPCLIENT(...)
|
||||||
@ -121,6 +121,7 @@ class HTTPClient {
|
|||||||
|
|
||||||
void setReuse(bool reuse); /// keep-alive
|
void setReuse(bool reuse); /// keep-alive
|
||||||
void setUserAgent(const char * userAgent);
|
void setUserAgent(const char * userAgent);
|
||||||
|
void setAuthorization(const char * user, const char * password);
|
||||||
|
|
||||||
/// request handling
|
/// request handling
|
||||||
int GET();
|
int GET();
|
||||||
@ -172,6 +173,7 @@ class HTTPClient {
|
|||||||
|
|
||||||
String _Headers;
|
String _Headers;
|
||||||
String _userAgent;
|
String _userAgent;
|
||||||
|
String _base64Authorization;
|
||||||
|
|
||||||
/// Response handling
|
/// Response handling
|
||||||
RequestArgument* _currentHeaders;
|
RequestArgument* _currentHeaders;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user