From: Scott Chacon <schacon@gmail.com>
To: git list <git@vger.kernel.org>
Cc: Junio C Hamano <gitster@pobox.com>,
"Shawn O. Pearce" <spearce@spearce.org>
Subject: [PATCH] Prompt for a username when an HTTP request 401s
Date: Thu, 1 Apr 2010 13:29:04 -0700 [thread overview]
Message-ID: <o2xd411cc4a1004011329wcbe34a45he7f3d7ce5d4eae59@mail.gmail.com> (raw)
When an HTTP request returns a 401, Git will currently fail with a
confusing message saying that it got a 401. This changes
http_request to prompt for the username and password, then return
HTTP_REAUTH so http_get_strbuf can try again. If it gets a 401 even
when a user/pass is supplied, http_request will now return HTTP_NOAUTH
which remote_curl can then use to display a more intelligent error
message that is less confusing.
Signed-off-by: Scott Chacon <schacon@gmail.com>
---
Here is the fourth version of this patch - now incorporating the
GIT_ASKPASS stuff.
http.c | 20 ++++++++++++++++++--
http.h | 2 ++
remote-curl.c | 2 ++
3 files changed, 22 insertions(+), 2 deletions(-)
diff --git a/http.c b/http.c
index 4814217..6027546 100644
--- a/http.c
+++ b/http.c
@@ -815,7 +815,19 @@ static int http_request(const char *url, void
*result, int target, int options)
ret = HTTP_OK;
else if (missing_target(&results))
ret = HTTP_MISSING_TARGET;
- else
+ else if (results.http_code == 401) {
+ if (user_name) {
+ ret = HTTP_NOAUTH;
+ } else {
+ // git_getpass is needed here because its very likely stdin/stdout are
+ // pipes to our parent process. So we instead need to use /dev/tty,
+ // but that is non-portable. Using git_getpass() can at least be stubbed
+ // on other platforms with a different implementation if/when necessary.
+ user_name = xstrdup(git_getpass("Username: "));
+ init_curl_http_auth(slot->curl);
+ ret = HTTP_REAUTH;
+ }
+ } else
ret = HTTP_ERROR;
} else {
error("Unable to start HTTP request for %s", url);
@@ -831,7 +843,11 @@ static int http_request(const char *url, void
*result, int target, int options)
int http_get_strbuf(const char *url, struct strbuf *result, int options)
{
- return http_request(url, result, HTTP_REQUEST_STRBUF, options);
+ int http_ret = http_request(url, result, HTTP_REQUEST_STRBUF, options);
+ if (http_ret == HTTP_REAUTH) {
+ http_ret = http_request(url, result, HTTP_REQUEST_STRBUF, options);
+ }
+ return http_ret;
}
/*
diff --git a/http.h b/http.h
index 5c9441c..2dd03e8 100644
--- a/http.h
+++ b/http.h
@@ -126,6 +126,8 @@ extern char *get_remote_object_url(const char
*url, const char *hex,
#define HTTP_MISSING_TARGET 1
#define HTTP_ERROR 2
#define HTTP_START_FAILED 3
+#define HTTP_REAUTH 4
+#define HTTP_NOAUTH 5
/*
* Requests an url and stores the result in a strbuf.
diff --git a/remote-curl.c b/remote-curl.c
index b76bfcb..0782756 100644
--- a/remote-curl.c
+++ b/remote-curl.c
@@ -132,6 +132,8 @@ static struct discovery* discover_refs(const char *service)
case HTTP_MISSING_TARGET:
die("%s not found: did you run git update-server-info on the"
" server?", refs_url);
+ case HTTP_NOAUTH:
+ die("Authentication failed");
default:
http_error(refs_url, http_ret);
die("HTTP request failed");
--
1.7.0.1
next reply other threads:[~2010-04-01 20:29 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-04-01 20:29 Scott Chacon [this message]
2010-04-01 21:30 ` [PATCH] Prompt for a username when an HTTP request 401s Junio C Hamano
2010-04-01 22:06 ` Scott Chacon
-- strict thread matches above, loose matches on Subject: below --
2010-04-01 22:14 Scott Chacon
2010-04-02 6:39 ` Junio C Hamano
2010-04-02 15:43 ` Scott Chacon
2010-04-02 16:11 ` Junio C Hamano
2010-03-19 19:17 Scott Chacon
2010-03-19 3:41 Scott Chacon
2010-03-19 9:13 ` Tay Ray Chuan
2010-03-19 9:34 ` Daniel Stenberg
2010-03-19 14:16 ` Shawn O. Pearce
2010-03-19 14:32 ` Shawn O. Pearce
2010-03-19 19:08 ` Scott Chacon
2010-03-19 19:09 ` Shawn O. Pearce
2010-03-19 19:27 ` Junio C Hamano
2010-03-18 18:57 Scott Chacon
2010-03-18 19:03 ` Shawn O. Pearce
2010-03-18 23:53 ` René Scharfe
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=o2xd411cc4a1004011329wcbe34a45he7f3d7ce5d4eae59@mail.gmail.com \
--to=schacon@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=spearce@spearce.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).