kioslave: use slave-defined error enum where appropriate in http slave

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-05-13 01:31:00 +03:00
parent 12e9b231b7
commit 06407334e6
4 changed files with 27 additions and 298 deletions

View file

@ -242,15 +242,13 @@ void HttpProtocol::mimetype(const KUrl &url)
CURLcode curlresult = curl_easy_setopt(m_curl, CURLOPT_NOBODY, 1L);
if (curlresult != CURLE_OK) {
kWarning(7103) << curl_easy_strerror(curlresult);
error(KIO::ERR_CONNECTION_BROKEN, curl_easy_strerror(curlresult));
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(curlresult));
return;
}
curlresult = curl_easy_perform(m_curl);
if (curlresult != CURLE_OK) {
kWarning(7103) << curl_easy_strerror(curlresult);
error(curlToKIOError(curlresult), curl_easy_strerror(curlresult));
error(curlToKIOError(curlresult), url.prettyUrl());
return;
}
@ -272,8 +270,7 @@ void HttpProtocol::stat(const KUrl &url)
// NOTE: do not set CURLOPT_NOBODY, server may not send some headers
CURLcode curlresult = curl_easy_perform(m_curl);
if (curlresult != CURLE_OK) {
kWarning(7103) << curl_easy_strerror(curlresult);
error(curlToKIOError(curlresult), curl_easy_strerror(curlresult));
error(curlToKIOError(curlresult), url.prettyUrl());
return;
}
@ -304,8 +301,7 @@ void HttpProtocol::get(const KUrl &url)
CURLcode curlresult = curl_easy_perform(m_curl);
if (curlresult != CURLE_OK) {
kWarning(7103) << curl_easy_strerror(curlresult);
error(curlToKIOError(curlresult), curl_easy_strerror(curlresult));
error(curlToKIOError(curlresult), url.prettyUrl());
return;
}
@ -322,8 +318,8 @@ void HttpProtocol::slotMIME()
setMetaData(QString::fromLatin1("modified"), m_httpheader.get(QLatin1String("Last-Modified")));
if (m_httpheader.status() >= 400) {
kDebug(7103) << "HTTP error" << m_httpheader.status() << m_httpheader.errorString();
error(HTTPToKIOError(m_httpheader.status()), m_httpheader.errorString());
kDebug(7103) << "HTTP error" << m_httpheader.status();
error(HTTPToKIOError(m_httpheader.status()), m_url.prettyUrl());
return;
}
@ -373,6 +369,8 @@ void HttpProtocol::slotProgress(KIO::filesize_t received, KIO::filesize_t total)
bool HttpProtocol::setupCurl(const KUrl &url)
{
m_url = url;
if (Q_UNLIKELY(!m_curl)) {
error(KIO::ERR_OUT_OF_MEMORY, QString::fromLatin1("Null context"));
return false;
@ -397,22 +395,19 @@ bool HttpProtocol::setupCurl(const KUrl &url)
const QByteArray urlbytes = url.prettyUrl().toLocal8Bit();
CURLcode curlresult = curl_easy_setopt(m_curl, CURLOPT_URL, urlbytes.constData());
if (curlresult != CURLE_OK) {
kWarning(7103) << curl_easy_strerror(curlresult);
error(KIO::ERR_MALFORMED_URL, curl_easy_strerror(curlresult));
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(curlresult));
return false;
}
curlresult = curl_easy_setopt(m_curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
if (curlresult != CURLE_OK) {
kWarning(7103) << curl_easy_strerror(curlresult);
error(KIO::ERR_CONNECTION_BROKEN, curl_easy_strerror(curlresult));
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(curlresult));
return false;
}
curlresult = curl_easy_setopt(m_curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
if (curlresult != CURLE_OK) {
kWarning(7103) << curl_easy_strerror(curlresult);
error(KIO::ERR_CONNECTION_BROKEN, curl_easy_strerror(curlresult));
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(curlresult));
return false;
}
@ -422,7 +417,8 @@ bool HttpProtocol::setupCurl(const KUrl &url)
const QByteArray useragentbytes = metaData("UserAgent").toAscii();
curlresult = curl_easy_setopt(m_curl, CURLOPT_USERAGENT, useragentbytes.constData());
if (curlresult != CURLE_OK) {
kWarning(7103) << curl_easy_strerror(curlresult);
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(curlresult));
return false;
}
}
@ -434,34 +430,37 @@ bool HttpProtocol::setupCurl(const KUrl &url)
kDebug(7103) << "Proxy" << proxybytes << curlproxytype;
curlresult = curl_easy_setopt(m_curl, CURLOPT_PROXY, proxybytes.constData());
if (curlresult != CURLE_OK) {
kWarning(7103) << curl_easy_strerror(curlresult);
error(KIO::ERR_UNKNOWN_PROXY_HOST, curl_easy_strerror(curlresult));
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(curlresult));
return false;
}
curlresult = curl_easy_setopt(m_curl, CURLOPT_PROXYTYPE, curlproxytype);
if (curlresult != CURLE_OK) {
kWarning(7103) << curl_easy_strerror(curlresult);
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(curlresult));
return false;
}
const bool noproxyauth = (noauth || metaData("no-proxy-auth") == QLatin1String("yes"));
kDebug(7103) << "Proxy auth" << noproxyauth;
curlresult = curl_easy_setopt(m_curl, CURLOPT_PROXYAUTH, noproxyauth ? CURLAUTH_NONE : CURLAUTH_ANY);
if (curlresult != CURLE_OK) {
kWarning(7103) << curl_easy_strerror(curlresult);
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(curlresult));
return false;
}
}
kDebug(7103) << "Auth" << noauth;
curlresult = curl_easy_setopt(m_curl, CURLOPT_HTTPAUTH, noauth ? CURLAUTH_NONE : CURLAUTH_ANY);
if (curlresult != CURLE_OK) {
kWarning(7103) << curl_easy_strerror(curlresult);
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(curlresult));
return false;
}
if (hasMetaData(QLatin1String("referrer"))) {
const QByteArray referrerbytes = metaData("referrer").toAscii();
curlresult = curl_easy_setopt(m_curl, CURLOPT_REFERER, referrerbytes.constData());
if (curlresult != CURLE_OK) {
kWarning(7103) << curl_easy_strerror(curlresult);
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(curlresult));
return false;
}
}
@ -469,7 +468,8 @@ bool HttpProtocol::setupCurl(const KUrl &url)
const qlonglong resumeoffset = metaData(QLatin1String("resume")).toLongLong();
curlresult = curl_easy_setopt(m_curl, CURLOPT_RESUME_FROM_LARGE, resumeoffset);
if (curlresult != CURLE_OK) {
kWarning(7103) << curl_easy_strerror(curlresult);
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(curlresult));
return false;
} else {
canResume();
}
@ -496,8 +496,7 @@ bool HttpProtocol::setupCurl(const KUrl &url)
if (curlresult != CURLE_OK) {
curl_slist_free_all(m_curlheaders);
m_curlheaders = nullptr;
kWarning(7103) << curl_easy_strerror(curlresult);
error(KIO::ERR_CONNECTION_BROKEN, curl_easy_strerror(curlresult));
error(KIO::ERR_SLAVE_DEFINED, curl_easy_strerror(curlresult));
return false;
}

View file

@ -49,6 +49,7 @@ private:
CURL* m_curl;
struct curl_slist *m_curlheaders;
KUrl m_url;
KHTTPHeader m_httpheader;
};

View file

@ -54,7 +54,7 @@ void KHTTPHeader::remove(const QString &field)
void KHTTPHeader::clear()
{
d->m_status = HTTP_Unknown;
d->m_status = 0;
d->m_map.clear();
}
@ -97,199 +97,3 @@ int KHTTPHeader::status() const
{
return d->m_status;
}
QString KHTTPHeader::errorString() const
{
switch (status()) {
case HTTP_Continue: {
return QString::fromLatin1("The server has received the request headers and the client should proceed to send the request body");
}
case HTTP_SwitchingProtocol: {
return QString::fromLatin1("The requester has asked the server to switch protocols and the server has agreed to do so");
}
case HTTP_Processing: {
return QString::fromLatin1("A WebDAV request may contain many sub-requests involving file operations, requiring a long time to complete the request");
}
case HTTP_EarlyHints: {
return QString::fromLatin1("Used to return some response headers before final HTTP message");
}
case HTTP_Ok: {
return QString::fromLatin1("Standard response for successful HTTP requests");
}
case HTTP_Created: {
return QString::fromLatin1("The request has been fulfilled, resulting in the creation of a new resource");
}
case HTTP_Accepted: {
return QString::fromLatin1("The request has been accepted for processing, but the processing has not been completed");
}
case HTTP_NonAuthoritativeInformation: {
return QString::fromLatin1("The server is a transforming proxy");
}
case HTTP_NoContent: {
return QString::fromLatin1("The server successfully processed the request, and is not returning any content");
}
case HTTP_ResetContent: {
return QString::fromLatin1("The server successfully processed the request, asks that the requester reset its document view, and is not returning any content");
}
case HTTP_PartialContent: {
return QString::fromLatin1("The server is delivering only part of the resource (byte serving) due to a range header sent by the client");
}
case HTTP_MultiStatus: {
return QString::fromLatin1("The message body that follows is by default an XML message and can contain a number of separate response codes, depending on how many sub-requests were made");
}
case HTTP_AlreadyReported: {
return QString::fromLatin1("The members of a DAV binding have already been enumerated in a preceding part of the (multistatus) response, and are not being included again");
}
case HTTP_IMUsed: {
return QString::fromLatin1("The server has fulfilled a request for the resource, and the response is a representation of the result of one or more instance-manipulations applied to the current instance");
}
case HTTP_MultipleChoices: {
return QString::fromLatin1("Indicates multiple options for the resource from which the client may choose (via agent-driven content negotiation)");
}
case HTTP_MovedPermanently: {
return QString::fromLatin1("This and all future requests should be directed to the given URI");
}
case HTTP_Found: {
return QString::fromLatin1("Tells the client to look at (browse to) another URL");
}
case HTTP_SeeOther: {
return QString::fromLatin1("The response to the request can be found under another URI using the GET method");
}
case HTTP_NotModified: {
return QString::fromLatin1("Indicates that the resource has not been modified since the version specified by the request headers If-Modified-Since or If-None-Match");
}
case HTTP_UseProxy: {
return QString::fromLatin1("The requested resource is available only through a proxy, the address for which is provided in the response");
}
case HTTP_SwitchProxy: {
return QString::fromLatin1("Subsequent requests should use the specified proxy");
}
case HTTP_TemporaryRedirect: {
return QString::fromLatin1("In this case, the request should be repeated with another URI; however, future requests should still use the original URI");
}
case HTTP_PermanentRedirect: {
return QString::fromLatin1("This and all future requests should be directed to the given URI");
}
case HTTP_BadRequest: {
return QString::fromLatin1("The server cannot or will not process the request due to an apparent client error");
}
case HTTP_Unauthorized: {
return QString::fromLatin1("Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet been provided");
}
case HTTP_PaymentRequired: {
return QString::fromLatin1("Reserved for future use");
}
case HTTP_Forbidden: {
return QString::fromLatin1("The request contained valid data and was understood by the server, but the server is refusing action");
}
case HTTP_NotFound: {
return QString::fromLatin1("The requested resource could not be found but may be available in the future");
}
case HTTP_MethodNotAllowed: {
return QString::fromLatin1("A request method is not supported for the requested resource");
}
case HTTP_NotAcceptable: {
return QString::fromLatin1("The requested resource is capable of generating only content not acceptable according to the Accept headers sent in the request");
}
case HTTP_ProxyAuthenticationRequired: {
return QString::fromLatin1("The client must first authenticate itself with the proxy");
}
case HTTP_RequestTimeout: {
return QString::fromLatin1("The server timed out waiting for the request");
}
case HTTP_Conflict: {
return QString::fromLatin1("Indicates that the request could not be processed because of conflict in the current state of the resource");
}
case HTTP_Gone: {
return QString::fromLatin1("Indicates that the resource requested is no longer available and will not be available again");
}
case HTTP_LengthRequired: {
return QString::fromLatin1("The request did not specify the length of its content, which is required by the requested resource");
}
case HTTP_PreconditionFailed: {
return QString::fromLatin1("The server does not meet one of the preconditions that the requester put on the request header fields");
}
case HTTP_PayloadTooLarge: {
return QString::fromLatin1("The request is larger than the server is willing or able to process");
}
case HTTP_URITooLong: {
return QString::fromLatin1("The URI provided was too long for the server to process");
}
case HTTP_UnsupportedMediaType: {
return QString::fromLatin1("The request entity has a media type which the server or resource does not support");
}
case HTTP_RangeNotSatisfiable: {
return QString::fromLatin1("The client has asked for a portion of the file (byte serving), but the server cannot supply that portion");
}
case HTTP_ExpectationFailed: {
return QString::fromLatin1("The server cannot meet the requirements of the Expect request-header field");
}
case HTTP_Imateapot: {
return QString::fromLatin1("This code was defined in 1998 as one of the traditional IETF April Fools' jokes");
}
case HTTP_MisdirectedRequest: {
return QString::fromLatin1("The request was directed at a server that is not able to produce a response");
}
case HTTP_UnprocessableEntity: {
return QString::fromLatin1("The request was well-formed but was unable to be followed due to semantic errors");
}
case HTTP_Locked: {
return QString::fromLatin1("The resource that is being accessed is locked");
}
case HTTP_FailedDependency: {
return QString::fromLatin1("The request failed because it depended on another request and that request failed");
}
case HTTP_TooEarly: {
return QString::fromLatin1("Indicates that the server is unwilling to risk processing a request that might be replayed");
}
case HTTP_UpgradeRequired: {
return QString::fromLatin1("The client should switch to a different protocol such as TLS/1.3, given in the Upgrade header field");
}
case HTTP_PreconditionRequired: {
return QString::fromLatin1("The origin server requires the request to be conditional");
}
case HTTP_TooManyRequests: {
return QString::fromLatin1("The user has sent too many requests in a given amount of time");
}
case HTTP_RequestHeaderFieldsTooLarge: {
return QString::fromLatin1("The server is unwilling to process the request because either an individual header field, or all the header fields collectively, are too large");
}
case HTTP_UnavailableForLegalReasons: {
return QString::fromLatin1("A server operator has received a legal demand to deny access to a resource or to a set of resources that includes the requested resource");
}
case HTTP_InternalServerError: {
return QString::fromLatin1("A generic error message, given when an unexpected condition was encountered and no more specific message is suitable");
}
case HTTP_NotImplemented: {
return QString::fromLatin1("The server either does not recognize the request method, or it lacks the ability to fulfil the request");
}
case HTTP_BadGateway: {
return QString::fromLatin1("The server was acting as a gateway or proxy and received an invalid response from the upstream server");
}
case HTTP_ServiceUnavailable: {
return QString::fromLatin1("The server cannot handle the request (because it is overloaded or down for maintenance)");
}
case HTTP_GatewayTimeout: {
return QString::fromLatin1("The server was acting as a gateway or proxy and did not receive a timely response from the upstream server");
}
case HTTP_HTTPVersionNotSupported: {
return QString::fromLatin1("The server does not support the HTTP protocol version used in the request");
}
case HTTP_VariantAlsoNegotiates: {
return QString::fromLatin1("Transparent content negotiation for the request results in a circular reference");
}
case HTTP_InsufficientStorage: {
return QString::fromLatin1("The server is unable to store the representation needed to complete the request");
}
case HTTP_LoopDetected: {
return QString::fromLatin1("The server detected an infinite loop while processing the request (sent instead of 208 Already Reported).");
}
case HTTP_NotExtended: {
return QString::fromLatin1("Further extensions to the request are required for the server to fulfil it");
}
case HTTP_NetworkAuthenticationRequired: {
return QString::fromLatin1("The client needs to authenticate to gain network access");
}
}
return QString::fromLatin1("Unknown error");
}

View file

@ -26,80 +26,6 @@ class KHTTPHeaderPrivate;
class KHTTPHeader
{
public:
// for reference:
// https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
enum HTTPStatus {
HTTP_Unknown = 0,
// 1xx informational response
HTTP_Continue = 100,
HTTP_SwitchingProtocol = 101,
HTTP_Processing = 102,
HTTP_EarlyHints = 103,
// 2xx successful
HTTP_Ok = 200,
HTTP_Created = 201,
HTTP_Accepted = 202,
HTTP_NonAuthoritativeInformation = 203,
HTTP_NoContent = 204,
HTTP_ResetContent = 205,
HTTP_PartialContent = 206,
HTTP_MultiStatus = 207,
HTTP_AlreadyReported = 208,
HTTP_IMUsed = 226,
// 3xx redirection
HTTP_MultipleChoices = 300,
HTTP_MovedPermanently = 301,
HTTP_Found = 302,
HTTP_SeeOther = 303,
HTTP_NotModified = 304,
HTTP_UseProxy = 305,
HTTP_SwitchProxy = 306,
HTTP_TemporaryRedirect = 307,
HTTP_PermanentRedirect = 308,
// 4xx client error
HTTP_BadRequest = 400,
HTTP_Unauthorized = 401,
HTTP_PaymentRequired = 402,
HTTP_Forbidden = 403,
HTTP_NotFound = 404,
HTTP_MethodNotAllowed = 405,
HTTP_NotAcceptable = 406,
HTTP_ProxyAuthenticationRequired = 407,
HTTP_RequestTimeout = 408,
HTTP_Conflict = 409,
HTTP_Gone = 410,
HTTP_LengthRequired = 411,
HTTP_PreconditionFailed = 412,
HTTP_PayloadTooLarge = 413,
HTTP_URITooLong = 414,
HTTP_UnsupportedMediaType = 415,
HTTP_RangeNotSatisfiable = 416,
HTTP_ExpectationFailed = 417,
HTTP_Imateapot = 418,
HTTP_MisdirectedRequest = 421,
HTTP_UnprocessableEntity = 422,
HTTP_Locked = 423,
HTTP_FailedDependency = 424,
HTTP_TooEarly = 425,
HTTP_UpgradeRequired = 426,
HTTP_PreconditionRequired = 428,
HTTP_TooManyRequests = 429,
HTTP_RequestHeaderFieldsTooLarge = 431,
HTTP_UnavailableForLegalReasons = 451,
// 5xx server error
HTTP_InternalServerError = 500,
HTTP_NotImplemented = 501,
HTTP_BadGateway = 502,
HTTP_ServiceUnavailable = 503,
HTTP_GatewayTimeout = 504,
HTTP_HTTPVersionNotSupported = 505,
HTTP_VariantAlsoNegotiates = 506,
HTTP_InsufficientStorage = 507,
HTTP_LoopDetected = 508,
HTTP_NotExtended = 510,
HTTP_NetworkAuthenticationRequired = 511
};
KHTTPHeader();
~KHTTPHeader();
@ -110,7 +36,6 @@ public:
void parseHeader(const QByteArray &header);
int status() const;
QString errorString() const;
private:
Q_DISABLE_COPY(KHTTPHeader);