From 0366901e50be6e489868b08b187c6a891e93cfb2 Mon Sep 17 00:00:00 2001
From: tonio <tonio@pkgsrc.org>
Date: Fri, 18 Nov 2011 21:06:19 +0000
Subject: Fix problem with msn connect, reported by Frédéric Fauberteau
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The patch is obtained using revisions 824 and 825 of the bitlbee repository
---
 chat/bitlbee/Makefile                         |  5 +-
 chat/bitlbee/distinfo                         |  3 +-
 chat/bitlbee/patches/patch-lib_http__client.c | 66 +++++++++++++++++++++++++++
 3 files changed, 71 insertions(+), 3 deletions(-)
 create mode 100644 chat/bitlbee/patches/patch-lib_http__client.c

diff --git a/chat/bitlbee/Makefile b/chat/bitlbee/Makefile
index f76a3b6e50c..45df1ceb959 100644
--- a/chat/bitlbee/Makefile
+++ b/chat/bitlbee/Makefile
@@ -1,14 +1,15 @@
-# $NetBSD: Makefile,v 1.54 2011/09/25 19:07:48 joerg Exp $
+# $NetBSD: Makefile,v 1.55 2011/11/18 21:06:19 tonio Exp $
 #
 
 DISTNAME=		bitlbee-3.0.3
-PKGREVISION=		1
+PKGREVISION=		2
 CATEGORIES=		chat
 MASTER_SITES=		http://get.bitlbee.org/src/
 
 MAINTAINER=		tonio@NetBSD.org
 HOMEPAGE=		http://www.bitlbee.org/
 COMMENT=		IRC to other chat networks gateway
+LICENSE=		gnu-gpl-v2
 
 PKG_DESTDIR_SUPPORT=	user-destdir
 
diff --git a/chat/bitlbee/distinfo b/chat/bitlbee/distinfo
index 400993bb3c0..f8dfe68f06d 100644
--- a/chat/bitlbee/distinfo
+++ b/chat/bitlbee/distinfo
@@ -1,5 +1,6 @@
-$NetBSD: distinfo,v 1.27 2011/07/09 11:13:20 mspo Exp $
+$NetBSD: distinfo,v 1.28 2011/11/18 21:06:19 tonio Exp $
 
 SHA1 (bitlbee-3.0.3.tar.gz) = 4140eb7aaa2c6a39fa059d19f8fbaec0d7a1ebff
 RMD160 (bitlbee-3.0.3.tar.gz) = 4b537871eef1bb5b6839105bc7bc9f2567e5b8e1
 Size (bitlbee-3.0.3.tar.gz) = 676417 bytes
+SHA1 (patch-lib_http__client.c) = 9659b12ee2ad796cab9ac4fa9b29067677df63fe
diff --git a/chat/bitlbee/patches/patch-lib_http__client.c b/chat/bitlbee/patches/patch-lib_http__client.c
new file mode 100644
index 00000000000..96e0c8ef99d
--- /dev/null
+++ b/chat/bitlbee/patches/patch-lib_http__client.c
@@ -0,0 +1,66 @@
+$NetBSD: patch-lib_http__client.c,v 1.1 2011/11/18 21:06:19 tonio Exp $
+
+Fix for MSN login troubles
+
+--- lib/http_client.c	(revision devel,783)
++++ lib/http_client.c	(revision devel,825)
+@@ -314,5 +314,5 @@
+ 	}
+ 	
+-	if( ( req->status_code == 301 || req->status_code == 302 ) && req->redir_ttl-- > 0 )
++	if( ( req->status_code >= 301 && req->status_code <= 303 ) && req->redir_ttl-- > 0 )
+ 	{
+ 		char *loc, *new_request, *new_host;
+@@ -354,4 +354,5 @@
+ 			url_t *url;
+ 			char *s;
++			const char *new_method;
+ 			
+ 			s = strstr( loc, "\r\n" );
+@@ -369,25 +370,34 @@
+ 			}
+ 			
+-			/* Okay, this isn't fun! We have to rebuild the request... :-( */
+-			new_request = g_malloc( req->request_length + strlen( url->file ) );
+-			
+-			/* So, now I just allocated enough memory, so I'm
+-			   going to use strcat(), whether you like it or not. :-) */
+-			
+-			sprintf( new_request, "GET %s HTTP/1.0", url->file );
+-			
+-			s = strstr( req->request, "\r\n" );
+-			if( s == NULL )
++			/* Find all headers and, if necessary, the POST request contents.
++			   Skip the old Host: header though. This crappy code here means
++			   anything using this http_client MUST put the Host: header at
++			   the top. */
++			if( !( ( s = strstr( req->request, "\r\nHost: " ) ) &&
++			       ( s = strstr( s + strlen( "\r\nHost: " ), "\r\n" ) ) ) )
+ 			{
+ 				req->status_string = g_strdup( "Error while rebuilding request string" );
+-				g_free( new_request );
+ 				g_free( url );
+ 				goto cleanup;
+ 			}
+ 			
+-			strcat( new_request, s );
++			/* More or less HTTP/1.0 compliant, from my reading of RFC 2616.
++			   Always perform a GET request unless we received a 301. 303 was
++			   meant for this but it's HTTP/1.1-only and we're specifically
++			   speaking HTTP/1.0. */
++			new_method = req->status_code != 301 || req->request[0] == 'G' ? "GET" : "POST";
++			
++			/* Okay, this isn't fun! We have to rebuild the request... :-( */
++			new_request = g_strdup_printf( "%s %s HTTP/1.0\r\nHost: %s%s",
++			                               new_method, url->file, url->host, s );
++			
+ 			new_host = g_strdup( url->host );
+ 			new_port = url->port;
+ 			new_proto = url->proto;
++			
++			/* If we went from POST to GET, truncate the request content. */
++			if( new_request[0] != req->request[0] && new_request[0] == 'G' &&
++			    ( s = strstr( new_request, "\r\n\r\n" ) ) )
++				s[4] = '\0';
+ 			
+ 			g_free( url );
-- 
cgit v1.2.3