10000 Merge pull request #4508 from libgit2/ethomson/user_agent · libgit2/libgit2@408b16c · GitHub
[go: up one dir, main page]

Skip to content

Commit 408b16c

Browse files
authored
Merge pull request #4508 from libgit2/ethomson/user_agent
http: standardize user-agent addition
2 parents 05c24c4 + ee6be19 commit 408b16c

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

src/transports/http.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "remote.h"
1818
#include "smart.h"
1919
#include "auth.h"
20+
#include "http.h"
2021
#include "auth_negotiate.h"
2122
#include "streams/tls.h"
2223
#include "streams/socket.h"
@@ -190,16 +191,6 @@ static int apply_credentials(git_buf *buf, http_subtransport *t)
190191
return context->next_token(buf, context, cred);
191192
}
192193

193-
static const char *user_agent(void)
194-
{
195-
const char *custom = git_libgit2__user_agent();
196-
197-
if (custom)
198-
return custom;
199-
200-
return "libgit2 " LIBGIT2_VERSION;
201-
}
202-
203194
static int gen_request(
204195
git_buf *buf,
205196
http_stream *s,
@@ -211,7 +202,9 @@ static int gen_request(
211202

212203
git_buf_printf(buf, "%s %s%s HTTP/1.1\r\n", s->verb, path, s->service_url);
213204

214-
git_buf_printf(buf, "User-Agent: git/2.0 (%s)\r\n", user_agent());
205+
git_buf_puts(buf, "User-Agent: ");
206+
git_http__user_agent(buf);
207+
git_buf_puts(buf, "\r\n");
215208
git_buf_printf(buf, "Host: %s\r\n", t->connection_data.host);
216209

217210
if (s->chunked || content_length > 0) {

src/transports/http.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#ifndef INCLUDE_transports_http_h__
9+
#define INCLUDE_transports_http_h__
10+
11+
#include "buffer.h"
12+
13+
GIT_INLINE(int) git_http__user_agent(git_buf *buf)
14+
{
15+
const char *ua = git_libgit2__user_agent();
16+
17+
if (!ua)
18+
ua = "libgit2 " LIBGIT2_VERSION;
19+
20+
return git_buf_printf(buf, "git/2.0 (%s)", ua);
21+
}
22+
23+
#endif

src/transports/winhttp.c

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "remote.h"
1919
#include "repository.h"
2020
#include "global.h"
21+
#include "http.h"
2122

2223
#include <wincrypt.h>
2324
#include <winhttp.h>
@@ -701,21 +702,6 @@ static int winhttp_close_connection(winhttp_subtransport *t)
701702
return ret;
702703
}
703704

704-
static int user_agent(git_buf *ua)
705-
{
706-
const char *custom = git_libgit2__user_agent();
707-
708-
git_buf_clear(ua);
709-
git_buf_PUTS(ua, "git/1.0 (");
710-
711-
if (custom)
712-
git_buf_puts(ua, custom);
713-
else
714-
git_buf_PUTS(ua, "libgit2 " LIBGIT2_VERSION);
715-
716-
return git_buf_putc(ua, ')');
717-
}
718-
719705
static void CALLBACK winhttp_status(
720706
HINTERNET connection,
721707
DWORD_PTR ctx,
@@ -772,7 +758,8 @@ static int winhttp_connect(
772758
return -1;
773759
}
774760

775-
if ((error = user_agent(&ua)) < 0) {
761+
762+
if ((error = git_http__user_agent(&ua)) < 0) {
776763
git__free(wide_host);
777764
return error;
778765
}

0 commit comments

Comments
 (0)
0