1
0
mirror of https://github.com/arduino-libraries/ArduinoHttpClient.git synced 2025-04-19 21:22:15 +03:00

Add URL Encoder class

This commit is contained in:
Sandeep Mistry 2019-04-08 15:05:59 -04:00 committed by Sandeep Mistry
parent 4045c856e4
commit d73940758a
4 changed files with 82 additions and 0 deletions

View File

@ -9,6 +9,7 @@
ArduinoHttpClient KEYWORD1 ArduinoHttpClient KEYWORD1
HttpClient KEYWORD1 HttpClient KEYWORD1
WebSocketClient KEYWORD1 WebSocketClient KEYWORD1
URLEncoder KEYWORD1
####################################### #######################################
# Methods and Functions (KEYWORD2) # Methods and Functions (KEYWORD2)
@ -47,6 +48,8 @@ isFinal KEYWORD2
readString KEYWORD2 readString KEYWORD2
ping KEYWORD2 ping KEYWORD2
encode KEYWORD2
####################################### #######################################
# Constants (LITERAL1) # Constants (LITERAL1)
####################################### #######################################

View File

@ -7,5 +7,6 @@
#include "HttpClient.h" #include "HttpClient.h"
#include "WebSocketClient.h" #include "WebSocketClient.h"
#include "URLEncoder.h"
#endif #endif

53
src/URLEncoder.cpp Normal file
View File

@ -0,0 +1,53 @@
// Library to simplify HTTP fetching on Arduino
// (c) Copyright Arduino. 2019
// Released under Apache License, version 2.0
#include "URLEncoder.h"
URLEncoderClass::URLEncoderClass()
{
}
URLEncoderClass::~URLEncoderClass()
{
}
String URLEncoderClass::encode(const char* str)
{
return encode(str, strlen(str));
}
String URLEncoderClass::encode(const String& str)
{
return encode(str.c_str(), str.length());
}
String URLEncoderClass::encode(const char* str, int length)
{
String encoded;
encoded.reserve(length);
for (int i = 0; i < length; i++) {
char c = str[i];
const char HEX_DIGIT_MAPPER[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
if (isAlphaNumeric(c) || (c == '-') || (c == '.') || (c == '_') || (c == '~')) {
encoded += c;
} else {
char s[4];
s[0] = '%';
s[1] = HEX_DIGIT_MAPPER[(c >> 4) & 0xf];
s[2] = HEX_DIGIT_MAPPER[(c & 0x0f)];
s[3] = 0;
encoded += s;
}
}
return encoded;
}
URLEncoderClass URLEncoder;

25
src/URLEncoder.h Normal file
View File

@ -0,0 +1,25 @@
// Library to simplify HTTP fetching on Arduino
// (c) Copyright Arduino. 2019
// Released under Apache License, version 2.0
#ifndef URL_ENCODER_H
#define URL_ENCODER_H
#include <Arduino.h>
class URLEncoderClass
{
public:
URLEncoderClass();
virtual ~URLEncoderClass();
String encode(const char* str);
String encode(const String& str);
private:
String encode(const char* str, int length);
};
extern URLEncoderClass URLEncoder;
#endif