Skip to content

Commit 3f12831

Browse files
committed
Merge pull request #1181 from Links2004/httpClient
HTTPClient add Authorization support
2 parents 5cd42a0 + 0a4da83 commit 3f12831

File tree

9 files changed

+357
-28
lines changed

9 files changed

+357
-28
lines changed

cores/esp8266/base64.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* base64.cpp
3+
*
4+
* Created on: 09.12.2015
5+
*
6+
* Copyright (c) 2015 Markus Sattler. All rights reserved.
7+
* This file is part of the ESP8266 core for Arduino.
8+
*
9+
* This library is free software; you can redistribute it and/or
10+
* modify it under the terms of the GNU Lesser General Public
11+
* License as published by the Free Software Foundation; either
12+
* version 2.1 of the License, or (at your option) any later version.
13+
*
14+
* This library is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
* Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public
20+
* License along with this library; if not, write to the Free Software
21+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22+
*
23+
*/
24+
25+
#include "Arduino.h"
26+
extern "C" {
27+
#include "libb64/cdecode.h"
28+
#include "libb64/cencode.h"
29+
}
30+
#include "base64.h"
31+
32+
/**
33+
* convert input data to base64
34+
* @param data uint8_t *
35+
* @param length size_t
36+
* @return String
37+
*/
38+
String base64::encode(uint8_t * data, size_t length) {
39+
// base64 needs more size then the source data
40+
size_t size = ((length * 1.6f) + 1);
41+
char * buffer = (char *) malloc(size);
42+
if(buffer) {
43+
base64_encodestate _state;
44+
base64_init_encodestate(&_state);
45+
int len = base64_encode_block((const char *) &data[0], length, &buffer[0], &_state);
46+
len = base64_encode_blockend((buffer + len), &_state);
47+
48+
String base64 = String(buffer);
49+
free(buffer);
50+
return base64;
51+
}
52+
return String("-FAIL-");
53+
}
54+
55+
/**
56+
* convert input data to base64
57+
* @param text String
58+
* @return String
59+
*/
60+
String base64::encode(String text) {
61+
return base64::encode((uint8_t *) text.c_str(), text.length());
62+
}
63+

cores/esp8266/base64.h

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* base64.h
3+
*
4+
* Created on: 09.12.2015
5+
*
6+
* Copyright (c) 2015 Markus Sattler. All rights reserved.
7+
* This file is part of the ESP8266 core for Arduino.
8+
*
9+
* This library is free software; you can redistribute it and/or
10+
* modify it under the terms of the GNU Lesser General Public
11+
* License as published by the Free Software Foundation; either
12+
* version 2.1 of the License, or (at your option) any later version.
13+
*
14+
* This library is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
* Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public
20+
* License along with this library; if not, write to the Free Software
21+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22+
*
23+
*/
24+
25+
#ifndef CORE_BASE64_H_
26+
#define CORE_BASE64_H_
27+
28+
class base64 {
29+
public:
30+
static String encode(uint8_t * data, size_t length);
31+
static String encode(String text);
32+
private:
33+
};
34+
35+
36+
#endif /* CORE_BASE64_H_ */

cores/esp8266/core_esp8266_features.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727

2828
#define CORE_HAS_LIBB64
29+
#define CORE_HAS_BASE64_CLASS
2930

3031

3132
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Authorization.ino
3+
*
4+
* Created on: 09.12.2015
5+
*
6+
*/
7+
8+
#include <Arduino.h>
9+
10+
#include <ESP8266WiFi.h>
11+
#include <ESP8266WiFiMulti.h>
12+
13+
#include <ESP8266HTTPClient.h>
14+
15+
#define USE_SERIAL Serial
16+
17+
ESP8266WiFiMulti WiFiMulti;
18+
19+
void setup() {
20+
21+
USE_SERIAL.begin(115200);
22+
// USE_SERIAL.setDebugOutput(true);
23+
24+
USE_SERIAL.println();
25+
USE_SERIAL.println();
26+
USE_SERIAL.println();
27+
28+
for(uint8_t t = 4; t > 0; t--) {
29+
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
30+
USE_SERIAL.flush();
31+
delay(1000);
32+
}
33+
34+
WiFiMulti.addAP("SSID", "PASSWORD");
35+
36+
}
37+
38+
void loop() {
39+
// wait for WiFi connection
40+
if((WiFiMulti.run() == WL_CONNECTED)) {
41+
42+
HTTPClient http;
43+
44+
USE_SERIAL.print("[HTTP] begin...\n");
45+
// configure traged server and url
46+
47+
48+
http.begin("http://user:[email protected]/test.html");
49+
50+
/*
51+
// or
52+
http.begin("http://192.168.1.12/test.html");
53+
http.setAuthorization("user", "password");
54+
55+
// or
56+
http.begin("http://192.168.1.12/test.html");
57+
http.setAuthorization("dXNlcjpwYXN3b3Jk");
58+
*/
59+
60+
61+
USE_SERIAL.print("[HTTP] GET...\n");
62+
// start connection and send HTTP header
63+
int httpCode = http.GET();
64+
65+
// httpCode will be negative on error
66+
if(httpCode) {
67+
// HTTP header has been send and Server response header has been handled
68+
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
69+
70+
// file found at server
71+
if(httpCode == HTTP_CODE_OK) {
72+
String payload = http.getString();
73+
USE_SERIAL.println(payload);
74+
}
75+
} else {
76+
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
77+
}
78+
79+
http.end();
80+
}
81+
82+
delay(10000);
83+
}
84+

libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino

+8-4
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,28 @@ void loop() {
4343

4444
USE_SERIAL.print("[HTTP] begin...\n");
4545
// configure traged server and url
46-
//http.begin("192.168.1.12", 443, "/test.html", true, "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
47-
http.begin("192.168.1.12", 80, "/test.html"); //HTTP
46+
//http.begin("https://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
47+
http.begin("http://192.168.1.12/test.html"); //HTTP
4848

4949
USE_SERIAL.print("[HTTP] GET...\n");
5050
// start connection and send HTTP header
5151
int httpCode = http.GET();
52+
53+
// httpCode will be negative on error
5254
if(httpCode) {
5355
// HTTP header has been send and Server response header has been handled
5456
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
5557

5658
// file found at server
57-
if(httpCode == 200) {
59+
if(httpCode == HTTP_CODE_OK) {
5860
String payload = http.getString();
5961
USE_SERIAL.println(payload);
6062
}
6163
} else {
62-
USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
64+
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
6365
}
66+
67+
http.end();
6468
}
6569

6670
delay(10000);

libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino

+9-4
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,31 @@ void setup() {
3636

3737
WiFiMulti.addAP("SSID", "PASSWORD");
3838

39-
39+
// allow reuse (if server supports it)
40+
http.setReuse(true);
4041
}
4142

4243
void loop() {
4344
// wait for WiFi connection
4445
if((WiFiMulti.run() == WL_CONNECTED)) {
4546

46-
http.begin("192.168.1.12", 80, "/test.html");
47+
http.begin("http://192.168.1.12/test.html");
48+
//http.begin("192.168.1.12", 80, "/test.html");
4749

4850
int httpCode = http.GET();
4951
if(httpCode) {
5052
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
5153

5254
// file found at server
53-
if(httpCode == 200) {
55+
if(httpCode == HTTP_CODE_OK) {
5456
http.writeToStream(&USE_SERIAL);
5557
}
5658
} else {
57-
USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
59+
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
5860
}
61+
62+
http.end();
63+
5964
}
6065

6166
delay(1000);

libraries/ESP8266HTTPClient/examples/StreamHttpClient/StreamHttpClient.ino

+8-5
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,20 @@ void loop() {
4242
HTTPClient http;
4343

4444
USE_SERIAL.print("[HTTP] begin...\n");
45-
// configure traged server and url
46-
http.begin("192.168.1.12", 80, "/test.html");
45+
46+
// configure server and url
47+
http.begin("http://192.168.1.12/test.html");
48+
//http.begin("192.168.1.12", 80, "/test.html");
4749

4850
USE_SERIAL.print("[HTTP] GET...\n");
4951
// start connection and send HTTP header
5052
int httpCode = http.GET();
5153
if(httpCode) {
5254
// HTTP header has been send and Server response header has been handled
53-
5455
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
5556

5657
// file found at server
57-
if(httpCode == 200) {
58+
if(httpCode == HTTP_CODE_OK) {
5859

5960
// get lenght of document (is -1 when Server sends no Content-Length header)
6061
int len = http.getSize();
@@ -89,8 +90,10 @@ void loop() {
8990

9091
}
9192
} else {
92-
USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
93+
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
9394
}
95+
96+
http.end();
9497
}
9598

9699
delay(10000);

0 commit comments

Comments
 (0)