/**
ByteBuffer
HTTPRequest.cpp
Copyright 2011-2021 Ramsey Kant
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "HTTPMessage.h"
#include "HTTPRequest.h"
HTTPRequest::HTTPRequest() : HTTPMessage() {
this->init();
}
HTTPRequest::HTTPRequest(std::string sData) : HTTPMessage(sData) {
this->init();
}
HTTPRequest::HTTPRequest(byte* pData, unsigned int len) : HTTPMessage(pData, len) {
this->init();
}
HTTPRequest::~HTTPRequest() {
}
void HTTPRequest::init() {
method = 0;
requestUri = "";
}
/**
* Takes the method name and converts it to the corresponding method
* id detailed in the Method enum
*
* @param name String representation of the Method
* @return Corresponding Method ID, -1 if unable to find the method
*/
int HTTPRequest::methodStrToInt(std::string name) {
// Method name cannot must be between 1 and 10 characters. Anything outside those bounds shouldn't be compared at all
if (name.empty() || (name.size() >= 10))
return -1;
// Loop through requestMethodStr array and attempt to match the 'name' with a known method in the array
int ret = -1;
for (unsigned int i = 0; i < NUM_METHODS; i++) {
if (strcmp(requestMethodStr[i], name.c_str()) == 0) {
ret = i;
break;
}
}
return ret;
}
/**
* Takes the method ID in the Method enum and returns the corresponding std::string representation
* @param mid Method ID to lookup
* @return The method name in the from of a std::string. Blank if unable to find the method
*/
std::string HTTPRequest::methodIntToStr(unsigned int mid) {
// ID is out of bounds of the possible requestMethodStr indexes
if (mid >= NUM_METHODS)
return "";
// Return the std::string matching the id
return requestMethodStr[mid];
}
/**
* Create
* Create and return a byte array of an HTTP request, built from the variables of this HTTPRequest
*
* @return Byte array of this HTTPRequest to be sent over the wire
*/
byte* HTTPRequest::create() {
// Clear the bytebuffer in the event this isn't the first call of create()
clear();
// Insert the initial line: