feat(android): the write transport, and an error that says why

TSyncClient.Put publishes a container to the Gitea contents API: token header,
JSON envelope with the file base64 inside, sha omitted rather than empty when
creating, branch omitted so the repo default is used. 409 becomes its own
status, ssConflict, because it means re-read and re-apply rather than retry.

The first PUT is where a mistake costs data, so the probe writes to
write-test.json rather than the vault. Same repository, same token, same
envelope, same concurrency check - and the vault is never at stake. Reachable
from a debug-only button; the page asks Delphi whether to show it.

Two mistakes of mine on the way, both about replacing information with a guess:

- the probe did not push the config first, unlike Sync, so a token that had
  just been pasted had not reached Delphi and the 403 blamed the wrong thing.
- 401 and 403 were rewritten as "token lacks write access", discarding Gitea's
  body. That is an interpretation, not an observation: the same code covers a
  read-only repository, a protected branch, a token from another account. The
  body says which - here, plainly, "token scope=read:repository".

Also recorded: the sha comparison now short-circuits (304 where it used to
decrypt), so the difference was case or whitespace, absorbed by SameText on
trimmed values. The offending character itself was never seen.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
r-zakarya
2026-08-17 17:47:55 +01:00
parent ae48fadb6b
commit 90e8fabf58
4 changed files with 191 additions and 2 deletions
+121 -1
View File
@@ -39,7 +39,11 @@ type
ssNoSnapshot, // 404 : pas d'instantane, OU jeton invalide
ssRefused, // 403 : HTTPS exige par le serveur
ssHttpError, // autre code
ssNetworkError // rien n'a repondu
ssNetworkError, // rien n'a repondu
/// 409 : le sha envoye n'est plus celui du fichier - un autre appareil a
/// publie entre notre lecture et notre ecriture. L'appelant doit relire,
/// refusionner et renvoyer, pas reessayer tel quel.
ssConflict
);
TSyncResult = record
@@ -69,6 +73,15 @@ type
/// n'y ressemble pas est de toute facon inutilisable.
function IsGitea: Boolean;
/// Publie un conteneur. ASha est celui du fichier qu'on a lu : Gitea s'en
/// sert pour refuser (409) une ecriture si quelqu'un d'autre a publie
/// entre-temps. Vide = creation d'un fichier qui n'existait pas.
///
/// GITEA SEULEMENT pour l'instant. L'endpoint PHP repond 405 au PUT, et
/// lui ajouter l'ecriture n'a plus d'interet maintenant que le depot fait
/// le meme travail avec un controle de concurrence eprouve.
function Put(const AContainerJson, ASha, AMessage: string): TSyncResult;
property BaseUrl: string read FBaseUrl write FBaseUrl;
property Token: string read FToken write FToken;
/// Volontairement court : sur mobile, un reseau qui ne repond pas doit
@@ -86,6 +99,24 @@ begin
Result := FBaseUrl.ToLower.Contains('/api/v1/repos/');
end;
/// Le sha du fichier ecrit, tire de la reponse au PUT : Gitea rend
/// { "content": { "sha": ... }, "commit": { ... } }. Vide si la forme change -
/// l'appelant relira, ce qui coute un appel mais ne perd rien.
function ShaFromPutResponse(const ABody: string): string;
var
LRoot, LContent: TJSONObject;
begin
Result := '';
LRoot := TJSONObject.ParseJSONValue(ABody) as TJSONObject;
if LRoot = nil then Exit;
try
if LRoot.TryGetValue<TJSONObject>('content', LContent) then
Result := LContent.GetValue<string>('sha', '');
finally
LRoot.Free;
end;
end;
/// Extrait le conteneur et le sha de la reponse de l'API des contenus. Rend
/// faux, avec la raison, si la reponse n'a pas la forme attendue - une URL de
/// dépôt juste a cote rend du JSON, mais pas celui-la.
@@ -137,6 +168,95 @@ begin
end;
end;
function TSyncClient.Put(const AContainerJson, ASha, AMessage: string): TSyncResult;
var
LHttp: THTTPClient;
LResp: IHTTPResponse;
LEnv: TJSONObject;
LReq, LResync: TStringStream;
LB64: TBase64Encoding;
begin
Result := Default(TSyncResult);
Result.Status := ssNetworkError;
if not IsGitea then
begin
Result.Status := ssHttpError;
Result.Error := 'Ecriture disponible seulement vers un depot Gitea';
Exit;
end;
if (FBaseUrl = '') or (FToken = '') then
begin
Result.Error := 'URL ou jeton non configure';
Exit;
end;
LEnv := TJSONObject.Create;
LB64 := TBase64Encoding.Create(0); // pas de coupure : voir PMA.Crypto
try
LEnv.AddPair('message', AMessage);
LEnv.AddPair('content',
LB64.EncodeBytesToString(TEncoding.UTF8.GetBytes(AContainerJson)));
// OMIS, PAS VIDE, quand on cree le fichier : Gitea lit la presence de la
// cle comme « remplace exactement cette version-la », et une chaine vide
// n'est pas la meme chose que son absence.
if ASha <> '' then LEnv.AddPair('sha', ASha);
// `branch` reste absent : le depot utilise alors sa branche par defaut,
// plutot qu'un nom qu'on devinerait.
LReq := TStringStream.Create(LEnv.ToJSON, TEncoding.UTF8);
LResync := TStringStream.Create('', TEncoding.UTF8);
try
LHttp := THTTPClient.Create;
try
LHttp.ConnectionTimeout := FTimeoutMs;
LHttp.ResponseTimeout := FTimeoutMs;
LHttp.HandleRedirects := False;
LHttp.CustomHeaders['Authorization'] := 'token ' + FToken;
LHttp.CustomHeaders['Content-Type'] := 'application/json';
try
LResp := LHttp.Put(FBaseUrl, LReq, LResync);
except
on E: Exception do
begin
Result.Status := ssNetworkError;
Result.Error := E.ClassName + ': ' + E.Message;
Exit;
end;
end;
Result.HttpCode := LResp.StatusCode;
case LResp.StatusCode of
200, 201:
begin
Result.Status := ssOK;
// Le sha du fichier VENANT D'ETRE ECRIT, a garder pour la
// prochaine ecriture - sans lui il faudrait relire pour publier.
Result.ETag := ShaFromPutResponse(LResync.DataString);
end;
409: Result.Status := ssConflict;
else
Result.Status := ssHttpError;
// LE CORPS, TOUJOURS. Il etait remplace par un message fige sur 401
// et 403 - « jeton sans droit d'ecriture » - ce qui est une
// interpretation, pas une observation : le meme code couvre un depot
// en lecture seule, une branche protegee, un jeton d'un autre compte.
// Gitea dit lequel dans son corps, et le taire envoie chercher a
// cote.
Result.Error := Format('HTTP %d %s - %s', [LResp.StatusCode,
LResp.StatusText, Copy(LResync.DataString, 1, 300)]);
end;
finally
LHttp.Free;
end;
finally
LResync.Free;
LReq.Free;
end;
finally
LB64.Free;
LEnv.Free;
end;
end;
constructor TSyncClient.Create(const ABaseUrl, AToken: string);
begin
inherited Create;