Clean up http client enough to replace curl in send-patch.

This commit is contained in:
Jordan Bancino 2023-04-01 02:46:07 +00:00
parent e592840c99
commit e0c8530b12
5 changed files with 73 additions and 16 deletions

View file

@ -52,8 +52,16 @@ static off_t
IoSeekFile(void *cookie, off_t offset, int whence) IoSeekFile(void *cookie, off_t offset, int whence)
{ {
FILE *fp = cookie; FILE *fp = cookie;
off_t ret = fseeko(fp, offset, whence);
return fseeko(fp, offset, whence); if (ret > -1)
{
return ftello(fp);
}
else
{
return ret;
}
} }
static int static int

View file

@ -521,6 +521,31 @@ StreamGets(Stream * stream, char *str, int size)
return str; return str;
} }
off_t
StreamSeek(Stream *stream, off_t offset, int whence)
{
off_t result;
if (!stream)
{
errno = EBADF;
return -1;
}
result = IoSeek(stream->io, offset, whence);
if (result < 0)
{
return result;
}
/* Successful seek; clear the buffers */
stream->rOff = 0;
stream->wLen = 0;
stream->ugLen = 0;
return result;
}
int int
StreamEof(Stream * stream) StreamEof(Stream * stream)
{ {

View file

@ -75,6 +75,9 @@ extern int
extern char * extern char *
StreamGets(Stream *, char *, int); StreamGets(Stream *, char *, int);
extern off_t
StreamSeek(Stream *, off_t, int);
extern int extern int
StreamEof(Stream *); StreamEof(Stream *);

View file

@ -24,13 +24,13 @@ readpwd() {
stty echo ctlecho stty echo ctlecho
} }
# Calls curl, setting the RETURN variable for the actual # Makes an HTTP request, setting the RETURN variable for the
# reply from the server and the ERROR_CODE variable for a # actual reply from the server and the ERROR_CODE variable
# HTTP error code. # for a HTTP error code.
curlec() { request() {
RETURN="$(curl -w "\n%{http_code}" "$@" 2>/dev/null)" RETURN=$(http -i "$@" 2>/dev/null)
ERROR_CODE="$(echo "$RETURN" | tail -n1)" ERROR_CODE=$(echo "$RETURN" | head -n1 | awk '{print $2}')
RETURN="$(echo "$RETURN" | sed '$d')" RETURN=$(echo "$RETURN" | sed '1,/^[[:space:]]*$/d')
} }
# Prompts user to login and gives out an access token to use # Prompts user to login and gives out an access token to use
@ -38,7 +38,7 @@ curlec() {
matrix_login() { matrix_login() {
# Check authentication methods # Check authentication methods
echo "Checking authentication methods..." echo "Checking authentication methods..."
curlec "$HS_BASE/_matrix/client/v3/login" request "$HS_BASE/_matrix/client/v3/login"
AUTH_METHODS=$RETURN AUTH_METHODS=$RETURN
if [ $ERROR_CODE -ne 200 ]; then if [ $ERROR_CODE -ne 200 ]; then
echo "Homeserver does not support login." echo "Homeserver does not support login."
@ -66,10 +66,11 @@ matrix_login() {
printf ' "password": %s' "$(json -e "$MXPW")" printf ' "password": %s' "$(json -e "$MXPW")"
printf '}' printf '}'
) )
curlec -X POST -d "$JSON" $HS_BASE/_matrix/client/v3/login request -X POST -d "$JSON" $HS_BASE/_matrix/client/v3/login
LOGIN=$RETURN LOGIN="$RETURN"
if [ $ERROR_CODE -ne 200 ]; then if [ $ERROR_CODE -ne 200 ]; then
echo "Login failed." echo "Login failed."
echo "$RETURN"
exit 1 exit 1
fi fi
ACCESS_TOKEN=$(echo "$LOGIN" | json -s "access_token->@decode") ACCESS_TOKEN=$(echo "$LOGIN" | json -s "access_token->@decode")
@ -81,10 +82,11 @@ matrix_logout() {
echo "No access token" echo "No access token"
exit 1 exit 1
fi fi
curlec -X POST $HS_BASE/_matrix/client/v3/logout -H "Authorization: Bearer $ACCESS_TOKEN" request -X POST -H "Authorization: Bearer $ACCESS_TOKEN" "$HS_BASE/_matrix/client/v3/logout"
LOGOUT=$RETURN LOGOUT=$RETURN
if [ $ERROR_CODE -ne 200 ]; then if [ $ERROR_CODE -ne 200 ]; then
echo "Logout failed." echo "Logout failed."
echo "$RETURN"
exit 1 exit 1
fi fi
echo "Logged out." echo "Logged out."
@ -98,13 +100,15 @@ send_patch() {
# We are sucessfully logged in as our user, now let's # We are sucessfully logged in as our user, now let's
# try to upload and post our patch # try to upload and post our patch
echo "$PATCHFILE" echo "$PATCHFILE"
curlec -X POST "$HS_BASE/_matrix/media/v3/upload" \ request -X POST \
-H "Content-Type: text/x-patch" \ -H "Content-Type: text/x-patch" \
-H "Authorization: Bearer $ACCESS_TOKEN" \ -H "Authorization: Bearer $ACCESS_TOKEN" \
-T "$PATCHFILE" -d "@$PATCHFILE" \
"$HS_BASE/_matrix/media/v3/upload"
MXCID=$RETURN MXCID=$RETURN
if [ $ERROR_CODE -ne 200 ]; then if [ $ERROR_CODE -ne 200 ]; then
echo "Upload failed." echo "Upload failed."
echo "$RETURN"
matrix_logout matrix_logout
exit 1 exit 1
fi fi
@ -123,7 +127,7 @@ send_patch() {
printf ' "url": %s' "$(json -e $MXCID)" printf ' "url": %s' "$(json -e $MXCID)"
printf '}' printf '}'
) )
curl -X PUT -d "$JSON" -H "Authorization: Bearer $ACCESS_TOKEN" \ http -X PUT -d "$JSON" -H "Authorization: Bearer $ACCESS_TOKEN" \
"$HS_BASE/_matrix/client/v3/rooms/$PATCHES_ROOM/send/m.room.message/$(date +%s)" \ "$HS_BASE/_matrix/client/v3/rooms/$PATCHES_ROOM/send/m.room.message/$(date +%s)" \
2>/dev/null >/dev/null && echo "Patch sent." 2>/dev/null >/dev/null && echo "Patch sent."
@ -140,7 +144,7 @@ if [ "$(basename "$PATCHFILE" .patch)" = "$PATCHFILE" ] || [ ! -f "$PATCHFILE" ]
fi fi
echo "Sending file '$PATCHFILE'" echo "Sending file '$PATCHFILE'"
echo "Checking homeserver's real address using .well-known..." echo "Checking homeserver's real address using .well-known..."
curlec "https://$HS_NAME/.well-known/matrix/client" request "https://$HS_NAME/.well-known/matrix/client"
case "$ERROR_CODE" in case "$ERROR_CODE" in
"200") "200")
WELL_KNOWN=$RETURN WELL_KNOWN=$RETURN

View file

@ -162,6 +162,7 @@ main(int argc, char **argv)
if (*data == '@') if (*data == '@')
{ {
Stream *in; Stream *in;
int len;
data++; data++;
@ -180,6 +181,22 @@ main(int argc, char **argv)
return 1; return 1;
} }
len = StreamSeek(in, 0, SEEK_END);
if (len > -1)
{
char *lenStr;
int nBytes;
StreamSeek(in, 0, SEEK_SET);
nBytes = snprintf(NULL, 0, "%d", len);
lenStr = Malloc(nBytes + 1);
snprintf(lenStr, nBytes + 1, "%d", len);
HttpRequestHeader(cx, "Content-Length", lenStr);
Free(lenStr);
}
HttpRequestSendHeaders(cx); HttpRequestSendHeaders(cx);
StreamCopy(in, HttpClientStream(cx)); StreamCopy(in, HttpClientStream(cx));
StreamClose(in); StreamClose(in);