mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-30 22:24:56 +00:00
Implement and use git_pkt_send_request
This makes it easier to send a requqest for an URL. It assumes there is a socket where the string should go out to. Make git_pkt_gen_proto accept a command parameter, which defaults to git-upload-pack Signed-off-by: Carlos Martín Nieto <carlos@cmartin.tk>
This commit is contained in:
parent
4e95ef0268
commit
c4d0fa85b1
@ -54,6 +54,7 @@ struct git_pkt_ref {
|
|||||||
/**
|
/**
|
||||||
* Create a git protocol request.
|
* Create a git protocol request.
|
||||||
*/
|
*/
|
||||||
int git_pkt_gen_proto(char **out, int *outlen, const char *url);
|
int git_pkt_gen_proto(char **out, int *outlen, const char *cmd, const char *url);
|
||||||
|
int git_pkt_send_request(int socket, const char *cmd, const char *url);
|
||||||
int git_pkt_parse_line(git_pkt **head, const char *line, const char **out, unsigned int len);
|
int git_pkt_parse_line(git_pkt **head, const char *line, const char **out, unsigned int len);
|
||||||
void git_pkt_free(git_pkt *pkt);
|
void git_pkt_free(git_pkt *pkt);
|
||||||
|
35
src/pkt.c
35
src/pkt.c
@ -29,6 +29,7 @@
|
|||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
#include "netops.h"
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
@ -207,10 +208,10 @@ void git_pkt_free(git_pkt *pkt)
|
|||||||
*
|
*
|
||||||
* TODO: the command should not be hard-coded
|
* TODO: the command should not be hard-coded
|
||||||
*/
|
*/
|
||||||
int git_pkt_gen_proto(char **out, int *outlen, const char *url)
|
int git_pkt_gen_proto(char **out, int *outlen, const char *cmd, const char *url)
|
||||||
{
|
{
|
||||||
char *delim, *repo, *ptr;
|
char *delim, *repo, *ptr;
|
||||||
char command[] = "git-upload-pack";
|
char default_command[] = "git-upload-pack";
|
||||||
char host[] = "host=";
|
char host[] = "host=";
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
@ -224,7 +225,10 @@ int git_pkt_gen_proto(char **out, int *outlen, const char *url)
|
|||||||
if (delim == NULL)
|
if (delim == NULL)
|
||||||
delim = strchr(url, '/');
|
delim = strchr(url, '/');
|
||||||
|
|
||||||
len = 4 + STRLEN(command) + 1 + strlen(repo) + 1 + STRLEN(host) + (delim - url) + 2;
|
if (cmd == NULL)
|
||||||
|
cmd = default_command;
|
||||||
|
|
||||||
|
len = 4 + strlen(cmd) + 1 + strlen(repo) + 1 + STRLEN(host) + (delim - url) + 2;
|
||||||
|
|
||||||
*out = git__malloc(len);
|
*out = git__malloc(len);
|
||||||
if (*out == NULL)
|
if (*out == NULL)
|
||||||
@ -234,7 +238,30 @@ int git_pkt_gen_proto(char **out, int *outlen, const char *url)
|
|||||||
ptr = *out;
|
ptr = *out;
|
||||||
memset(ptr, 0x0, len);
|
memset(ptr, 0x0, len);
|
||||||
/* We expect the return value to be > len - 1 so don't bother checking it */
|
/* We expect the return value to be > len - 1 so don't bother checking it */
|
||||||
snprintf(ptr, len -1, "%04x%s %s%c%s%s", len - 1, command, repo, 0, host, url);
|
snprintf(ptr, len -1, "%04x%s %s%c%s%s", len - 1, cmd, repo, 0, host, url);
|
||||||
|
|
||||||
return GIT_SUCCESS;
|
return GIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int git_pkt_send_request(int s, const char *cmd, const char *url)
|
||||||
|
{
|
||||||
|
int error, len;
|
||||||
|
char *msg = NULL;
|
||||||
|
|
||||||
|
error = git_pkt_gen_proto(&msg, &len, cmd, url);
|
||||||
|
if (error < GIT_SUCCESS)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
error = gitno_send(s, msg, len, 0);
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
free(msg);
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
int git_pkt_send_flush(int s)
|
||||||
|
{
|
||||||
|
char flush[] = "0000";
|
||||||
|
|
||||||
|
return gitno_send(s, flush, STRLEN(flush), 0);
|
||||||
|
}
|
||||||
|
@ -89,9 +89,9 @@ static int extract_host_and_port(char **host, char **port, const char *url)
|
|||||||
static int do_connect(git_priv *priv, const char *url)
|
static int do_connect(git_priv *priv, const char *url)
|
||||||
{
|
{
|
||||||
int s = -1;
|
int s = -1;
|
||||||
char *host, *port, *msg;
|
char *host, *port;
|
||||||
const char prefix[] = "git://";
|
const char prefix[] = "git://";
|
||||||
int error, msg_len, connected = 0;
|
int error, connected = 0;
|
||||||
|
|
||||||
if (!git__prefixcmp(url, prefix))
|
if (!git__prefixcmp(url, prefix))
|
||||||
url += STRLEN(prefix);
|
url += STRLEN(prefix);
|
||||||
@ -99,19 +99,9 @@ static int do_connect(git_priv *priv, const char *url)
|
|||||||
error = extract_host_and_port(&host, &port, url);
|
error = extract_host_and_port(&host, &port, url);
|
||||||
s = gitno_connect(host, port);
|
s = gitno_connect(host, port);
|
||||||
connected = 1;
|
connected = 1;
|
||||||
|
error = git_pkt_send_request(s, NULL, url);
|
||||||
error = git_pkt_gen_proto(&msg, &msg_len, url);
|
|
||||||
if (error < GIT_SUCCESS)
|
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
error = gitno_send(s, msg, msg_len, 0);
|
|
||||||
free(msg);
|
|
||||||
if (error < GIT_SUCCESS)
|
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
priv->socket = s;
|
priv->socket = s;
|
||||||
|
|
||||||
cleanup:
|
|
||||||
free(host);
|
free(host);
|
||||||
free(port);
|
free(port);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user