Bug 341716

Summary: uPnP does not work with some d-link routers [patch]
Product: [Applications] ktorrent Reporter: Osipov Oleg <legolegs>
Component: generalAssignee: Joris Guisson <joris.guisson>
Status: RESOLVED FIXED    
Severity: normal CC: arjunak234
Priority: NOR    
Version First Reported In: 4.3.1   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:
Attachments: Made HTTP header match case-insensitive. Fixes issue with d-link DIR-300

Description Osipov Oleg 2014-12-09 20:51:27 UTC
KTorrent uPnP plugin just don't see the d-link router model DIR-300 (and very likely many others). KTorrent/ktupnptest log looks like that:

Detected IGD Debian/squeeze/sid UPnP/1.0 MiniUPnPd/1.6
Downloading XML file 
Failed to download : Malformed URL

Note the absence of URL in log while it is supposed to be printed.

I found a solution for that issue.

Dlink uPnP service spells "location" header as "lOCATION" (with lower-case L, I almost spilled my coffee when found that) which confuses libktorrent expecting "Location", "location" or "LOCATION". I might be wrong but HTTP standard states headers are case-insensitive and thus d-link's MiniUPnPd behavior is silly but valid and ktorrent should accept such responses.

I paste a trivial patch in Additional Information section. I applied it and and uPnP now works for me.

Reproducible: Always

Steps to Reproduce:
1. Have d-link DIR-300
2. Activate uPnP in ktorrent

Actual Results:  
Nothing happens.

Expected Results:  
Ports being forwarded.

A typical D-Link MiniUPnPd response (it sends bunch of them with variations after "SERVER:" part):

=====================
NOTIFY * HTTP/1.1
HOST: 239.255.255.250:1900
CACHE-CONTROL: max-age=120
lOCATION: http://192.168.0.1:4649/rootDesc.xml
SERVER: Debian/squeeze/sid UPnP/1.0 MiniUPnPd/1.6
NT: urn:schemas-upnp-org:device:WANConnectionDevice:1
USN: uuid:00000000-466f-0000-0000-466f00000000::urn:schemas-upnp-org:device:WANConnectionDevice:1
NTS: ssdp:alive
OPT: "http://schemas.upnp.org/upnp/1/0/";
01-NLS: 1
BOOTID.UPNP.ORG: 1
CONFIGID.UPNP.ORG: 1337
=====================

Proposed patch:
=====================
From b9948b112132d1654aa6b52cc8b6ade908973130 Mon Sep 17 00:00:00 2001
From: Oleg Osipov <legolegs@yandex.ru>
Date: Tue, 9 Dec 2014 22:37:39 +0300
Subject: [PATCH] Made HTTP header match case-insensitive. Fixes issue with
 d-link DIR-300.

---
 src/upnp/upnpmcastsocket.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/upnp/upnpmcastsocket.cpp b/src/upnp/upnpmcastsocket.cpp
index 286952c..0c07a2e 100644
--- a/src/upnp/upnpmcastsocket.cpp
+++ b/src/upnp/upnpmcastsocket.cpp
@@ -343,13 +343,13 @@ namespace bt
                for (int i = 1;i < lines.count();i++)
                {
                        line = lines[i];
-                       if (line.startsWith("Location") || line.startsWith("LOCATION") || line.startsWith("location"))
+                       if (line.startsWith("location", Qt::CaseInsensitive))
                        {
                                location = line.mid(line.indexOf(':') + 1).trimmed();
                                if (!location.isValid())
                                        return 0;
                        }
-                       else if (line.startsWith("Server") || line.startsWith("server") || line.startsWith("SERVER"))
+                       else if (line.startsWith("server", Qt::CaseInsensitive))
                        {
                                server = line.mid(line.indexOf(':') + 1).trimmed();
                                if (server.length() == 0)
-- 
1.8.3.1

=====================
Comment 1 Osipov Oleg 2014-12-09 20:54:46 UTC
Created attachment 89890 [details]
Made HTTP header match case-insensitive. Fixes issue with d-link DIR-300
Comment 2 Arjun AK 2014-12-12 11:28:26 UTC
Please post the patch here: https://git.reviewboard.kde.org/r/new/
Comment 3 Osipov Oleg 2014-12-12 13:37:36 UTC
Done: https://git.reviewboard.kde.org/r/121463/
Comment 4 Martin Sandsmark 2016-10-06 21:39:25 UTC
Git commit ac4455532a73c1e98426b71a30c3f3e98c702463 by Martin T. H. Sandsmark.
Committed on 06/10/2016 at 21:38.
Pushed by sandsmark into branch 'master'.

Fix parsing of headers from certain D-Link routers

Dlink uPnP service spells "location" header as "lOCATION" (with
lower-case L) which confuses libktorrent expecting "Location",
"location" or "LOCATION". This patch makes uPnP plugin accept those
headers since they are valid.

Modified version of patch from Oleg Osipov.

REVIEW: 121463

M  +2    -2    src/upnp/upnpmcastsocket.cpp

http://commits.kde.org/libktorrent/ac4455532a73c1e98426b71a30c3f3e98c702463