From 8b65db69c4705adecf3ed8ea70cf460b836d9119 Mon Sep 17 00:00:00 2001 From: Gabriele Date: Sun, 15 Feb 2015 20:45:11 +0100 Subject: [PATCH] WebUI: Use regular expression to check if gzip is accepted The previous code caused a crash in case "gzip" was at the end of the string with no quality factor (;q=*) specified. --- src/core/http/connection.cpp | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/src/core/http/connection.cpp b/src/core/http/connection.cpp index 84985e895..ad757817c 100644 --- a/src/core/http/connection.cpp +++ b/src/core/http/connection.cpp @@ -31,6 +31,7 @@ #include #include +#include #include "types.h" #include "requestparser.h" #include "responsegenerator.h" @@ -85,26 +86,13 @@ void Connection::sendResponse(const Response &response) bool Connection::acceptsGzipEncoding(const QString &encoding) { - int pos = encoding.indexOf("gzip", 0, Qt::CaseInsensitive); - if (pos == -1) - return false; - - // Let's see if there's a qvalue of 0.0 following - if (encoding[pos + 4] != ';') //there isn't, so it accepts gzip anyway + QRegExp rx("(gzip)(;q=([^,]+))?"); + if (rx.indexIn(encoding) >= 0) { + if (rx.cap(2).size() > 0) + // check if quality factor > 0 + return (rx.cap(3).toDouble() > 0); + // if quality factor is not specified, then it's 1 return true; - - //So let's find = and the next comma - pos = encoding.indexOf("=", pos + 4, Qt::CaseInsensitive); - int comma_pos = encoding.indexOf(",", pos, Qt::CaseInsensitive); - - QString value; - if (comma_pos == -1) - value = encoding.mid(pos + 1, comma_pos); - else - value = encoding.mid(pos + 1, comma_pos - (pos + 1)); - - if (value.toDouble() == 0.0) - return false; - - return true; + } + return false; }