qrealka / http-status-codes-cpp

HTTP Status Codes and Reason Phrases for C, C++ and Qt

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HTTP Status Codes and Reason Phrases for C, C++ and Qt

Linux build status Windows build status

This repository provides the HTTP status codes and reason phrases in different variants for C/C++.

Simply take the variant that fits your needs and copy/use/modify it.

Data has been taken from for-GET/know-your-http-well.

Table of Contents

Variants

Variant Scoping Status Codes Reason Phrases
C Prefix HttpStatus_ enum HttpStatus_Code const char*
C++ Namespace HttpStatus enum Code std::string
C++11 Namespace HttpStatus enum class Code std::string
Qt Namespace HttpStatus enum Code
When using Qt 5.8 or later: registered in meta type system using Q_ENUM_NS()
QString

Example

#include "MyHttpReplyClass.h"
#include "HttpStatusCodes_C++.h"
#include <iostream>

void printReplyStatus(MyHttpReplyClass reply)
{
	if (reply.status == HttpStatus::OK)
		std::cout << "Success!";
	else
		std::cerr << reply.status << " " << HttpStatus::reasonPhrase(reply.status);
}

Documentation

Note: The header files contain more detailed Doxygen documentation for all types and functions.

Status Codes Enum

For the complete list of defined enums, see one of the header files.

C Variant

enum HttpStatus_Code
{
	HttpStatus_OK       = 200,
	HttpStatus_NotFound = 404 
	// ...
};

C++11 Variant

namespace HttpStatus {
enum class Code
{
	OK        = 200,
	Forbidden = 404
	// ...
};
}

Other Variants

namespace HttpStatus {
enum Code
{
	OK        = 200,
	Forbidden = 404
	// ...
};
}

Category/Class Tests

C Variant

char HttpStatus_isInformational(int code);
char HttpStatus_isSuccessful(int code);
char HttpStatus_isRedirection(int code);
char HttpStatus_isClientError(int code);
char HttpStatus_isServerError(int code);

Return 1 if the given code belongs to the corresponding class of status codes (see RFC7231). Return 0 otherwise.

char HttpStatus_isError(int code);

Returns 1 if the given code is either a client error, a server error or any non-standard error code. Non-standard error codes are status codes with a value of 600 or higher. Returns 0 otherwise.

Other Variants

Note: The C++11 variant also provides overloads for HttpStatus::Code. So there is no need to cast.

bool HttpStatus::isInformational(int code);
bool HttpStatus::isSuccessful(int code);
bool HttpStatus::isRedirection(int code);
bool HttpStatus::isClientError(int code);
bool HttpStatus::isServerError(int code);

Return true if the given code belongs to the corresponding class of status codes (see RFC7231). Return false otherwise.

bool HttpStatus::isError(int code);

Returns true if the given code is either a client error, a server error or any non-standard error code. Non-standard error codes are status codes with a value of 600 or higher. Returns false otherwise.

Reason Phrases

C Variant

const char* HttpStatus_reasonPhrase(int code);

Returns the HTTP reason phrase string corresponding to the given code.

C++/C++11 Variants

Note: The C++11 variant also provides an overload for HttpStatus::Code. So there is no need to cast.

std::string HttpStatus::reasonPhrase(int code);

Returns the HTTP reason phrase string corresponding to the given code.

Qt Variant

QString HttpStatus::reasonPhrase(int code);

Returns the HTTP reason phrase string corresponding to the given code.

QNetworkReply::NetworkError Mapping

Note: Available only in the Qt variant and if the Qt::Network module is available.

Qt Variant

int HttpStatus::networkErrorToStatusCode(QNetworkReply::NetworkError error);

Returns the HTTP status code corresponding to the given error if there is one. Otherwise, -1 is returned.

QNetworkReply::NetworkError HttpStatus::statusCodeToNetworkError(int code);

Returns the QNetworkReply::NetworkError corresponding to the given code if there is one. For codes where there is no exact match, the best matchnig "catch all" code (QNetworkReply::NoError, QNetworkReply::UnknownContentError, QNetworkReply::UnknownServerError or QNetworkReply::ProtocolFailure) is returned.

License

CC0
This project is licensed under Creative Commons CC0.

About

HTTP Status Codes and Reason Phrases for C, C++ and Qt

License:Other


Languages

Language:C++ 58.4%Language:C 24.3%Language:CMake 17.3%