Skip to content

Commit 19f13ef

Browse files
author
Grahame Grieve
committed
more work on SHL server
1 parent a280dcd commit 19f13ef

1 file changed

Lines changed: 64 additions & 72 deletions

File tree

server/endpoint_shl.pas

Lines changed: 64 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ function TSHLWebServer.processCreate(request: TIdHTTPRequestInfo; response: TIdH
120120
resp.str['pword'] := NewGuidId;
121121
resp.str['link'] := 'https://'+common.host+PathWithSlash+resp.str['uuid'];
122122

123-
c.SQL := 'Insert into SHL (uuid, pword, expiry, mimetype, vhl) values (:u, :p, :e, :m, :v)';
123+
c.SQL := 'Insert into SHL (uuid, pword, pin, expiry, vhl) values (:u, :p, :pin, :e, :v)';
124124
c.prepare;
125125
c.BindString('u', resp.str['uuid']);
126126
c.BindString('p', resp.str['pword']);
127+
c.BindStringOrNull('pin', resp.str['pin']);
127128
c.BindTimeStamp('e', DateTimeToTS(exp));
128-
c.BindString('m', req.str['mimetype']);
129129
c.BindIntegerFromBoolean('v', vhl);
130130
c.execute;
131131
c.terminate;
@@ -150,56 +150,59 @@ function TSHLWebServer.processCreate(request: TIdHTTPRequestInfo; response: TIdH
150150

151151
function TSHLWebServer.processUpload(request: TIdHTTPRequestInfo; response: TIdHTTPResponseInfo; c : TFDBConnection): String;
152152
var
153-
p : THTTPParameters;
154153
bytes, hcert : TBytes;
155-
req, resp : TJsonObject;
154+
req, resp, ff : TJsonObject;
155+
f : TJsonNode;
156156
begin
157157
result := 'upload SHL content';
158-
p := THTTPParameters.create(request.QueryParams, true);
158+
req := TJSONParser.Parse(request.PostStream);
159159
try
160-
req := TJSONParser.Parse(request.PostStream);
161-
try
162-
bytes := DecodeBase64(req.str['cnt']);
163-
hcert := DecodeBase64(req.str['hcert']);
164-
if (p.has('uuid') and p.has('pword')) then
160+
// in the request: a list of base64 encoded files, and maybe an hcert to sign
161+
hcert := DecodeBase64(req.str['hcert']);
162+
if (req.has('uuid') and req.has('pword')) then
163+
begin
164+
c.sql := 'select pword from SHL where uuid = '''+SQLWrapString(req['uuid'])+'''';
165+
c.Prepare;
166+
c.Execute;
167+
if not c.FetchNext then
168+
raise ERestfulException.create('processCreate', 404, itSecurity, 'uuid "'+req['uuid']+'" not found', nil);
169+
if req['pword'] <> c.ColStringByName['pword'] then
170+
raise ERestfulException.create('processCreate', 404, itSecurity, 'password failure', nil);
171+
c.terminate;
172+
c.SQL := 'Insert into SHLFiles (uuid, mimetype, content) values (:u, :m, :c)';
173+
c.prepare;
174+
for f in req.forceArr['files'] do
165175
begin
166-
c.sql := 'select pword from SHL where uuid = '''+SQLWrapString(p['uuid'])+'''';
167-
c.Prepare;
176+
ff := f as TJsonObject;
177+
bytes := DecodeBase64(ff.str['cnt']);
178+
c.BindString('u',req['uuid']);
179+
c.BindString('m', ff.str['mimetype']);
180+
c.BindBlob('c', bytes);
168181
c.Execute;
169-
if not c.FetchNext then
170-
raise ERestfulException.create('processCreate', 404, itSecurity, 'uuid "'+p['uuid']+'" not found', nil);
171-
if p['pword'] <> c.ColStringByName['pword'] then
172-
raise ERestfulException.create('processCreate', 404, itSecurity, 'password failure', nil);
173-
c.terminate;
174-
c.SQL := 'update SHL set blob = :b where uuid = '''+SQLWrapString(p['uuid'])+'''';
175-
c.prepare;
176-
c.BindBlob('b', bytes);
177-
c.Execute;
178-
c.terminate;
179-
response.ResponseNo := 200;
180-
response.ResponseText := 'OK';
181-
if hcert <> nil then
182-
begin
183-
resp := TJsonObject.create;
184-
try
185-
bytes := TJWTUtils.Sign_ES256(hcert, FVhlKey);
186-
resp['signature'] := EncodeBase64(bytes);
187-
resp['kid'] := FVhlKey.id;
188-
response.ContentText := TJSONWriter.writeObjectStr(resp, true);
189-
finally
190-
resp.free;
191-
end;
192-
end
193-
else
194-
response.ContentText := '{ "msg": "OK" }';
182+
end;
183+
c.terminate;
184+
response.ResponseNo := 200;
185+
response.ResponseText := 'OK';
186+
if hcert <> nil then
187+
begin
188+
resp := TJsonObject.create;
189+
try
190+
bytes := TJWTUtils.Sign_ES256(hcert, FVhlKey);
191+
resp['signature'] := EncodeBase64(bytes);
192+
resp['kid'] := FVhlKey.id;
193+
resp['msg'] := 'OK';
194+
response.ContentText := TJSONWriter.writeObjectStr(resp, true);
195+
finally
196+
resp.free;
197+
end;
195198
end
196199
else
197-
raise ERestfulException.create('processCreate', 404, itSecurity, 'uuid and/or pword not found', nil);
198-
finally
199-
req.free;
200-
end;
200+
response.ContentText := '{ "msg": "OK" }';
201+
end
202+
else
203+
raise ERestfulException.create('processCreate', 404, itSecurity, 'uuid and/or pword not found', nil);
201204
finally
202-
p.free;
205+
req.free;
203206
end;
204207
end;
205208

@@ -386,24 +389,33 @@ procedure TSHLWebEndPoint.checkDatabase;
386389
try
387390
m := c.FetchMetaData;
388391
try
389-
if not (m.HasTable('SHL')) then
392+
if not m.HasTable('SHLFiles') then
390393
begin
391394
c.StartTransact;
392395
try
393-
c.ExecSQL('CREATE TABLE Config( '+#13#10+
394-
' ConfigKey '+DBKeyType(c.owner.platform)+' '+ColCanBeNull(c.owner.platform, False)+', '+#13#10+
395-
' Value nchar(200) '+ColCanBeNull(c.owner.platform, False)+') '+CreateTableInfo(c.owner.platform));
396-
c.ExecSQL('Create INDEX SK_Config_ConfigKey ON Config (ConfigKey)');
397-
c.ExecSQL('Insert into Config (ConfigKey, Value) values (1, ''1'')'); // version
396+
c.DropTable('SHL');
397+
if not (m.HasTable('Config')) then
398+
begin
399+
c.ExecSQL('CREATE TABLE Config( '+#13#10+
400+
' ConfigKey '+DBKeyType(c.owner.platform)+' '+ColCanBeNull(c.owner.platform, False)+', '+#13#10+
401+
' Value nchar(200) '+ColCanBeNull(c.owner.platform, False)+') '+CreateTableInfo(c.owner.platform));
402+
c.ExecSQL('Create INDEX SK_Config_ConfigKey ON Config (ConfigKey)');
403+
c.ExecSQL('Insert into Config (ConfigKey, Value) values (2, ''2'')'); // version
404+
end;
398405
c.ExecSQL('CREATE TABLE SHL ( '+#13#10+
399406
' uuid nchar(40) '+ColCanBeNull(c.owner.platform, False)+', '+
400407
' pword nchar(40) '+ColCanBeNull(c.owner.platform, False)+', '+
401-
' mimetype nchar(60) '+ColCanBeNull(c.owner.platform, False)+', '+
408+
' pin nchar(40) '+ColCanBeNull(c.owner.platform, True)+', '+
402409
' expiry '+DBDateTimeType(c.owner.platform)+' '+ColCanBeNull(c.owner.platform, False)+', '+
403-
' vhl int '+ColCanBeNull(c.owner.platform, true)+', '+
404-
' blob '+DBBlobType(c.owner.platform)+' '+ColCanBeNull(c.owner.platform, true)+') '+
410+
' vhl int '+ColCanBeNull(c.owner.platform, true)+') '+
405411
CreateTableInfo(c.owner.platform));
406412
c.ExecSQL('Create INDEX SK_SHL_UUID ON SHL (uuid)');
413+
c.ExecSQL('CREATE TABLE SHLFiles ( '+#13#10+
414+
' uuid nchar(40) '+ColCanBeNull(c.owner.platform, False)+', '+
415+
' mimetype nchar '+ColCanBeNull(c.owner.platform, False)+', '+
416+
' content '+DBBlobType(c.owner.platform)+' '+ColCanBeNull(c.owner.platform, False)+') '+
417+
CreateTableInfo(c.owner.platform));
418+
c.ExecSQL('Create INDEX SK_SHLFILES_UUID ON SHLFiles (uuid)');
407419
c.Commit;
408420
except
409421
on e:exception do
@@ -415,26 +427,6 @@ procedure TSHLWebEndPoint.checkDatabase;
415427
end;
416428
end;
417429
end
418-
else
419-
begin
420-
t := m.GetTable('SHL');
421-
if not t.hasColumn('vhl') then
422-
begin
423-
c.StartTransact;
424-
try
425-
c.ExecSQL('ALTER TABLE SHL ADD vhl int '+ColCanBeNull(c.owner.platform, true));
426-
c.Commit;
427-
except
428-
on e:exception do
429-
begin
430-
Logging.log(e.message);
431-
c.Rollback;
432-
recordStack(e);
433-
raise;
434-
end;
435-
end;
436-
end;
437-
end;
438430
finally
439431
m.free;
440432
end;

0 commit comments

Comments
 (0)