diff --git a/delphi-backend/UMainForm.pas b/delphi-backend/UMainForm.pas index 21051d3..a2b7141 100644 --- a/delphi-backend/UMainForm.pas +++ b/delphi-backend/UMainForm.pas @@ -1419,6 +1419,11 @@ begin // send it as If-Match on the push so the server rejects (412) the // write when another device changed the file in between. var LIfMatch := GetParam('ifmatch'); + // GITEA, reconnu a l'URL. Meme transport, trois differences : le jeton + // remplace l'authentification Basic, le corps d'un PUT est du JSON que JS + // a deja mis en forme (le sha y voyage, pas dans un If-Match), et le test + // de connexion se fait par GET - l'API des contenus ne repond pas a HEAD. + var LGitea := LUrl.ToLower.Contains('/api/v1/repos/'); TThread.CreateAnonymousThread( procedure var @@ -1441,13 +1446,17 @@ begin try LHttp.ConnectionTimeout := 10000; LHttp.ResponseTimeout := 30000; - if (LUser <> '') then + // "token " et non "Bearer" : c'est la forme que Gitea attend + // d'un jeton d'acces personnel, et l'autre donne un 401 muet. + if LGitea then + LHttp.CustomHeaders['Authorization'] := 'token ' + LPwd + else if (LUser <> '') then begin LHttp.CredentialsStorage.AddCredential( System.Net.URLClient.TCredentialsStorage.TCredential.Create( System.Net.URLClient.TAuthTargetType.Server, '', '', LUser, LPwd)); end; - if LMethod = 'webdav/get' then + if (LMethod = 'webdav/get') or ((LMethod = 'webdav/test') and LGitea) then begin LBodyStream := TBytesStream.Create; try @@ -1470,15 +1479,31 @@ begin begin LBytes := TNetEncoding.Base64.DecodeStringToBytes(LData); LReqStream := TBytesStream.Create(LBytes); + // La reponse est LUE meme en echec : Gitea explique un 409 ou un + // 422 dans son corps, et sans cela le message d'erreur se + // reduisait au code, ce qui ne dit pas quoi corriger. + LBodyStream := TBytesStream.Create; try - if LIfMatch <> '' then - LResp := LHttp.Put(LUrl, LReqStream, nil, + if LGitea then + LResp := LHttp.Put(LUrl, LReqStream, LBodyStream, + [System.Net.URLClient.TNetHeader.Create('Content-Type', 'application/json')]) + else if LIfMatch <> '' then + LResp := LHttp.Put(LUrl, LReqStream, LBodyStream, [System.Net.URLClient.TNetHeader.Create('If-Match', LIfMatch)]) else - LResp := LHttp.Put(LUrl, LReqStream); + LResp := LHttp.Put(LUrl, LReqStream, LBodyStream); LStatus := LResp.StatusCode; LEtag := LResp.HeaderValue['ETag']; + if ((LStatus < 200) or (LStatus >= 300)) and (LBodyStream.Size > 0) then + begin + SetLength(LBytes, LBodyStream.Size); + Move(LBodyStream.Bytes[0], LBytes[0], LBodyStream.Size); + // Tronque : le corps d'une erreur tient en une phrase utile, + // et le reste n'a rien a faire dans un appel JavaScript. + LErr := Copy(TEncoding.UTF8.GetString(LBytes), 1, 300); + end; finally + LBodyStream.Free; LReqStream.Free; end; end @@ -1506,7 +1531,8 @@ begin // GET success path → ship body base64. Otherwise the field // carries either the empty string or the exception message // (for status=0 network errors). - if (LMethod = 'webdav/get') and (LStatus >= 200) and (LStatus < 300) then + if ((LMethod = 'webdav/get') or ((LMethod = 'webdav/test') and LGitea)) + and (LStatus >= 200) and (LStatus < 300) then LEscPayload := LBodyB64 else LEscPayload := LErr; diff --git a/index.html b/index.html index b9a49f7..d165809 100644 --- a/index.html +++ b/index.html @@ -756,10 +756,12 @@