Доработал обработку POST запросов для следующих MIME типов
- application/json
- application/x-www-form-urlencoded
- multipart/form-data
Теперь сервер умеет работать с json, принимать файлы, пока ещё не оч. большие (poststream блоками читается в строку = оперативную память), поэтому по-хорошему, размер файлов надо ограничить, ну и одновременную закачку файлов тоже, чтобы не подвесить память. Конечно же, стандартный form-url-encoded поддерживается, хотя если в multipart не указывать файл, просто параметры тоже прочитаются с декодированием из urlEncoded
Ну и в качестве бонуса научил сервер отправлять письма. Управлением памятью теперь почти везде занимается класс ISP<>, который находится в модуле uCommon.
Модуль декодирования POST запроса похудел и выглядит следующим образом
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
unit uDecodePostRequest; interface uses System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdBaseComponent, IdComponent, IdCustomTCPServer, IdCustomHTTPServer, IdHTTPServer, Vcl.StdCtrls, HTTPApp, IdMultipartFormData, IdContext, System.IOUtils, System.NetEncoding, superobject, Contnrs, uCommon, System.Generics.Collections, DateUtils, uUniqueName; type TPostParam = record Name: string; Value: string; end; type TDecodePostRequest = class private FRelWebFileDir: string; FPostParams: ISP<TList<TPostParam>>; FJson: string; //procedure ParseJson(const aAsObject: TSuperTableString); // uncomment if needed function ReadMultipartRequest(const aBoundary: string; aRequest: string; aHeader: ISP<TStringList>; var aData: string): string; procedure SetRelWebFileDir(const Value: string); public constructor Create(); procedure Multipart(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); procedure FormURLEncoded(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); procedure Execute(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); property RelWebFileDir: string read FRelWebFileDir write SetRelWebFileDir; property PostParams: ISP<TList<TPostParam>> read FPostParams; property Json: string read FJson; end; const MaxReadBlockSize = 8192; implementation uses System.StrUtils; procedure TDecodePostRequest.Execute(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); var ss: ISP<TStringStream>; begin AResponseInfo.Server := 'ver1'; AResponseInfo.CacheControl := 'no-cache'; if (Pos('multipart/form-data', LowerCase(ARequestInfo.ContentType)) > 0) and // далее идёт обработка multipart (Pos('boundary', LowerCase(ARequestInfo.ContentType)) > 0) then Multipart(AContext, ARequestInfo, AResponseInfo) else if (Pos('application/x-www-form-urlencoded', LowerCase(ARequestInfo.ContentType)) > 0) then FormURLEncoded(AContext, ARequestInfo, AResponseInfo) else if (Pos('application/json', LowerCase(ARequestInfo.ContentType)) > 0) then begin ss := TSP<TStringStream>.Create(); ss.LoadFromStream(ARequestInfo.PostStream); FJson := ss.DataString; // parse json... if needed or parse in other place //obj := SO(TNetEncoding.URL.Decode(allContent)); //ParseJson(obj.AsObject, aPostParams); end; end; procedure TDecodePostRequest.FormURLEncoded(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); var postParam: TPostParam; i: Integer; begin for i := 0 to ARequestInfo.Params.Count - 1 do begin postParam.Name := System.NetEncoding.TNetEncoding.URL.Decode(ARequestInfo.Params.Names[i]); postParam.Value := System.NetEncoding.TNetEncoding.URL.Decode(ARequestInfo.Params.Values[ARequestInfo.Params.Names[i]]); FPostParams.Add(postParam); end; end; { procedure TDecodePostRequest.ParseJson(const aAsObject: TSuperTableString); //https://stackoverflow.com/questions/14082886/superobject-extract-all var Names: ISuperObject; Name: string; Items: ISuperObject; Item: ISuperObject; idx: Integer; Value: string; begin if Assigned(aAsObject) then begin Names := aAsObject.GetNames; Items := aAsObject.GetValues; for idx := 0 to Items.AsArray.Length - 1 do begin Name := Names.AsArray[idx].AsString; Item := Items.AsArray[idx]; if Item.DataType = stObject then Value := '<Object>' else if Item.DataType = stArray then Value := '<Array>' else Value := Item.AsString; // do smth with json //postParams.Add(Name + '=' + Value); // postParams.json := Value; // returning only json object //if SameText(Name, 'id') then // WriteLn(Format('%s: %s', [aPrefix + Name, Value])); // if Item.DataType = stArray then // for ArrayItem in Item do // ProcessObject(ArrayItem.AsObject, aPrefix + Name + '.'); // if Item.DataType = stObject then // ProcessObject(Item.AsObject, aPrefix + Name + '.'); end; end; end; } //------------------------------------------------- function TDecodePostRequest.ReadMultipartRequest(const aBoundary: string; aRequest: string; aHeader: ISP<TStringList>; var aData: string): string; var Req, RHead: string; i: Integer; begin Result := ''; aHeader.Clear; aData := ''; if (Pos(aBoundary, aRequest) < Pos(aBoundary + '--', aRequest)) and (Pos(aBoundary, aRequest) = 1) then begin Delete(aRequest, 1, Length(aBoundary) + 2); Req := Copy(aRequest, 1, Pos(aBoundary, aRequest) - 3); Delete(aRequest, 1, Length(Req) + 2); RHead := Copy(Req, 1, Pos(#13#10#13#10, Req) - 1); Delete(Req, 1, Length(RHead) + 4); aHeader.Text := RHead; for i := 0 to aHeader.Count - 1 do if Pos(':', aHeader.Strings[i]) > 0 then aHeader.Strings[i] := Trim(Copy(aHeader.Strings[i], 1, Pos(':', aHeader.Strings[i]) - 1)) + '=' + Trim(Copy(aHeader.Strings[i], Pos(':', aHeader.Strings[i]) + 1, Length(aHeader.Strings[i]) - Pos(':', aHeader.Strings[i]))); aData := Req; Result := aRequest; end end; procedure TDecodePostRequest.SetRelWebFileDir(const Value: string); begin FRelWebFileDir := Value; end; procedure TDecodePostRequest.Multipart(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); function ValueByName(aName: string): string; var i: Integer; begin Result := ''; for i := 0 to FPostParams.Count - 1 do if FPostParams[i].Name = aName then begin result := FPostParams[i].Value; Break; end; end; function GetPostParam(aParamName: string): string; var value: string; begin value := ValueByName(aParamName); Result := IfThen(aParamName <> '', value, ''); end; const MaxReadBlockSize = 8192; var ms: ISP<TMemoryStream>; fs: ISP<TFileStream>; boundary, bufferStr, allContent: string; header: ISP<TStringList>; byteToRead, readedBytes, rSize: Integer; buffer: PAnsiChar; data: string; hList: ISP<TStringList>; filename: string; dir: string; isOverwrite: string; postParam: TPostParam; uploadDir: string; un: ISP<TUniqueName>; procedure ProcessUploadDir(); begin uploadDir := ExtractFilePath(Application.ExeName) + 'files\' + // YearOf(Now).ToString() + '\' + // MonthOf(Now).ToString() + '\' + // DayOf(Now).ToString(); // if (not TDirectory.Exists(uploadDir)) then TDirectory.CreateDirectory(uploadDir); end; begin AResponseInfo.Server := 'ver1'; AResponseInfo.CacheControl := 'no-cache'; // if (Pos('multipart/form-data', LowerCase(ARequestInfo.ContentType)) > 0) and if (Pos('boundary', LowerCase(ARequestInfo.ContentType)) > 0) then begin header := TSP<TStringList>.Create(); ExtractHeaderFields([';'], [' '], PChar(ARequestInfo.ContentType), header, False, False); boundary := header.Values['boundary']; // reading from postStream to allContent ms := TSP<TMemoryStream>.Create(); ms.LoadFromStream(ARequestInfo.PostStream); allContent := ''; byteToRead := ARequestInfo.ContentLength; while byteToRead > 0 do begin rSize := MaxReadBlockSize; if rSize > byteToRead then rSize := byteToRead; GetMem(buffer, rSize); try readedBytes := ms.Read(buffer^, rSize); SetString(bufferStr, buffer, readedBytes); allContent := allContent + bufferStr; finally FreeMem(buffer, rSize); end; byteToRead := ARequestInfo.ContentLength - Length(allContent); end; // reading post blocks (params) from allContent to FPostParams hList := TSP<TStringList>.Create(); if ARequestInfo.ContentLength = Length(allContent) then while Length(allContent) > Length('--' + boundary + '--' + #13#10) do begin header.Clear(); hList.Clear(); allContent := ReadMultipartRequest('--' + boundary, allContent, header, data); ExtractHeaderFields([';'], [' '], PChar(header.Values['Content-Disposition']), hList, False, True); postParam.Name := System.NetEncoding.TNetEncoding.URL.Decode(hList.Values['name']); postParam.Value := System.NetEncoding.TNetEncoding.URL.Decode(data); FPostParams.Add(postParam); end; // reading params filename := GetPostParam('filename'); dir := GetPostParam('dir'); isOverwrite := GetPostParam('isOverwrite'); data := GetPostParam('attach'); // writing file if (filename <> '') and (data <> '') then begin ProcessUploadDir(); if isOverwrite <> 'true' then begin un := TSP<TUniqueName>.Create(); filename := un.CreateUniqueNameAddingNumber(uploadDir, filename); end; fs := TSP<TFileStream>.Create(TFileStream.Create(uploadDir + '\' + filename, fmCreate)); try fs.WriteBuffer(Pointer(data)^, Length(data)); except on E: EStreamError do raise Exception.Create('EStreamError EClassName' + E.ClassName + ' ' + 'EMessage ' + E.Message); on E: Exception do raise Exception.Create('EClassName' + E.ClassName + ' ' + 'EMessage ' + E.Message); end; end; end; end; constructor TDecodePostRequest.Create(); begin FPostParams := TSP<TList<TPostParam>>.Create(); ; end; { TPostParams } end. |
Примеры отправки с клиента выглядят так
Для JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
procedure TClientExamples.PostJson; var client: ISP<TIdHTTP>; jo:ISuperobject; ss: ISP<TStringStream>; r: string; begin jo := SO; jo.S['param1'] := '1234'; jo.S['param2'] := '12345'; client := TSP<TIdHTTP>.Create(); ss := TSP<TStringStream>.Create(); ss.WriteString(jo.AsJSon(false, false)); client.Request.ContentType := 'application/json'; client.Request.ContentEncoding := 'utf-8'; r := client.Post('http://localhost:' + FPort + '/' + FRequest, ss); // Main.mAnswer.Lines.Add(r); end; |
Для multipart ( этот MIME тип создан для отправки файлов, ну и параметры, конечно тоже он читает, я их раскодирую из URLEncoded на всякий случай )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
procedure TClientExamples.PostSendFile(aAbsWinFilePath: string); var client: ISP<TIdHTTP>; ss: ISP<TStringStream>; postData: ISP<TIdMultiPartFormDataStream>; fileName: string; OpenDialog: TOpenDialog; begin fileName := ExtractFileName(aAbsWinFilePath); Assert(fileName <> '', 'filename is empty'); client := TSP<TIdHTTP>.Create(); ss := TSP<TStringStream>.Create(); postData := TSP<TIdMultiPartFormDataStream>.Create(); client.Request.Referer := 'http://localhost:' + FPort + '/Files/Send'; client.Request.ContentType := 'multipart/form-data'; client.Request.RawHeaders.AddValue('AuthToken', System.NetEncoding.TNetEncoding.URL.Encode('evjTI82N')); postData.AddFormField('filename', System.NetEncoding.TNetEncoding.URL.Encode(fileName)); postData.AddFormField('dir', System.NetEncoding.TNetEncoding.URL.Encode('files')); postData.AddFormField('isOverwrite', System.NetEncoding.TNetEncoding.URL.Encode('false')); postData.AddFile('attach', aAbsWinFilePath, 'application/x-rar-compressed'); client.POST('http://localhost:' + FPort + '/Files/Send', postData, ss); // Main.mAnswer.Lines.Add(ss.DataString); end; |
Для FormURLEncoded
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
procedure TClientExamples.PostFormURLEncoded(); var client: ISP<TIdHTTP>; sl: ISP<TStringList>; r: string; begin client := TSP<TIdHTTP>.Create(); sl := TSP<TStringList>.Create(); sl.Add('param1='+System.NetEncoding.TNetEncoding.URL.Encode('РусскийПараметр1')); sl.Add('param2='+System.NetEncoding.TNetEncoding.URL.Encode('РусскийПараметр2')); client.Request.Referer := 'http://localhost:' + FPort + '/Test/URLEncoded'; client.Request.ContentType := 'application/x-www-form-urlencoded'; client.Request.RawHeaders.AddValue('AuthToken', 'evjTI82N'); r := client.POST('http://localhost:' + FPort + '/Test/URLEncoded', sl); Main.mAnswer.Lines.Add(r); end; |