Changeset 138 for trunk/Packages/CoolWeb
- Timestamp:
- Sep 9, 2022, 8:20:25 PM (2 years ago)
- Location:
- trunk/Packages/CoolWeb
- Files:
-
- 1 added
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/CoolWeb/Common/UHtmlClasses.pas
r137 r138 4 4 5 5 uses 6 UXmlClasses, Classes, SysUtils, SpecializedList;6 UXmlClasses, Classes, SysUtils, Generics.Collections, UGenerics, UIpAddress; 7 7 8 8 type 9 10 { TDomainAddress } 11 12 TDomainAddress = class(TPersistent) 13 private 14 function GetAsString: string; 15 procedure SetAsString(const Value: string); 16 public 17 Levels: TListString; 18 constructor Create; 19 destructor Destroy; override; 20 property AsString: string read GetAsString write SetAsString; 21 end; 22 23 TAddrClass = (acA, acB, acC, acD, acE); 24 25 { TIpAddress } 26 27 TIpAddress = class(TPersistent) 28 private 29 function GetAddrClass: TAddrClass; 30 function GetAsCardinal: Cardinal; 31 function GetAsString: string; 32 function GetBroadcast: Boolean; 33 procedure SetBroadcast(const Value: Boolean); 34 procedure SetAsCardinal(const Value: Cardinal); 35 procedure SetAsString(const Value: string); 36 public 37 Octets: array[0..3] of Byte; 38 procedure Assign(Source: TPersistent); override; 39 function IsAddressString(Value: string): Boolean; 40 property AsCardinal: Cardinal read GetAsCardinal write SetAsCardinal; 41 property AsString: string read GetAsString write SetAsString; 42 property AddrClass: TAddrClass read GetAddrClass; 43 property Broadcast: Boolean read GetBroadcast write SetBroadcast; 44 end; 45 46 THostAddressState = (asDomainName, asIpAddress); 47 THostAddress = class(TPersistent) 48 private 49 function GetAsString: string; 50 procedure SetAsString(const Value: string); 51 public 52 State: THostAddressState; 53 DomainName: TDomainAddress; 54 IpAddress: TIpAddress; 55 constructor Create; 56 destructor Destroy; override; 57 property AsString: string read GetAsString write SetAsString; 58 end; 9 { TURL } 59 10 60 11 TURL = class(TPersistent) … … 90 41 end; 91 42 43 THtmlString = class; 44 THtmlBlock = class; 45 THtmlList = class; 46 THtmlInput = class; 47 48 { THtmlElements } 49 50 THtmlElements = class(TObjectList<THtmlElement>) 51 function AddString(Text: string = ''): THtmlString; 52 function AddBlock: THtmlBlock; 53 function AddList: THtmlList; 54 function AddInput: THtmlInput; 55 end; 56 92 57 TBlockType = (btNoTag, btBlockLevel, btInline); 93 58 … … 95 60 96 61 THtmlString = class(THtmlElement) 97 pr ivate62 protected 98 63 function GetAsXmlElement: TXmlElement; override; 99 64 public … … 105 70 106 71 THtmlLineBreak = class(THtmlElement) 107 pr ivate72 protected 108 73 function GetAsXmlElement: TXmlElement; override; 109 74 public … … 116 81 public 117 82 BlockType: TBlockType; 118 SubItems: TListObject; // TListObject<THtmlElement>; 83 SubItems: THtmlElements; 84 constructor Create; 85 destructor Destroy; override; 86 end; 87 88 TListType = (ltUnordered, ltOrdered); 89 90 { THtmlList } 91 92 THtmlList = class(THtmlElement) 93 protected 94 function GetAsXmlElement: TXmlElement; override; 95 public 96 ListType: TListType; 97 SubItems: THtmlElements; 119 98 constructor Create; 120 99 destructor Destroy; override; … … 122 101 123 102 THtmlLink = class(THtmlElement) 124 pr ivate103 protected 125 104 function GetAsXmlElement: TXmlElement; override; 126 105 public … … 140 119 141 120 THtmlImage = class(THtmlElement) 142 pr ivate121 protected 143 122 function GetAsXmlElement: TXmlElement; override; 144 123 public … … 156 135 157 136 THtmlInput = class(THtmlElement) 158 pr ivate137 protected 159 138 function GetAsXmlElement: TXmlElement; override; 160 139 public … … 197 176 198 177 THtmlCell = class(THtmlElement) 199 pr ivate178 protected 200 179 function GetAsXmlElement: TXmlElement; override; 201 180 public … … 207 186 end; 208 187 188 { THtmlCells } 189 190 THtmlCells = class(TObjectList<THtmlCell>) 191 function AddCell: THtmlCell; 192 end; 193 209 194 { THtmlRow } 210 195 211 196 THtmlRow = class(THtmlElement) 212 private 213 function GetAsXmlElement: TXmlElement; override; 214 public 215 Cells: TListObject; // TListObject<THtmlCell> 216 constructor Create; 217 destructor Destroy; override; 197 protected 198 function GetAsXmlElement: TXmlElement; override; 199 public 200 Cells: THtmlCells; 201 constructor Create; 202 destructor Destroy; override; 203 end; 204 205 { THtmlRows } 206 207 THtmlRows = class(TObjectList<THtmlRow>) 208 function AddRow: THtmlRow; 218 209 end; 219 210 … … 224 215 function GetAsXmlElement: TXmlElement; override; 225 216 public 226 Rows: T ListObject; // TListObject<THtmlRow>217 Rows: THtmlRows; 227 218 constructor Create; 228 219 destructor Destroy; override; … … 242 233 243 234 implementation 244 245 resourcestring246 SStringToIPConversionError = 'String to IP address conversion error';247 235 248 236 function LeftCutString(var Source, Output: string; Delimiter: string; Allowed: string = ''): Boolean; … … 298 286 end; 299 287 288 { THtmlCells } 289 290 function THtmlCells.AddCell: THtmlCell; 291 begin 292 Result := THtmlCell.Create; 293 Add(Result); 294 end; 295 296 { THtmlRows } 297 298 function THtmlRows.AddRow: THtmlRow; 299 begin 300 Result := THtmlRow.Create; 301 Add(Result); 302 end; 303 304 { THtmlList } 305 306 function THtmlList.GetAsXmlElement: TXmlElement; 307 var 308 I: Integer; 309 begin 310 Result := TXmlTag.Create; 311 with TXmlTag(Result) do begin 312 case ListType of 313 ltOrdered: Name := 'ol'; 314 ltUnordered: Name := 'ul'; 315 end; 316 for I := 0 to SubItems.Count - 1 do begin 317 with SubElements.AddTag('li') do 318 SubElements.Add(SubItems[I].AsXmlElement); 319 end; 320 end; 321 end; 322 323 constructor THtmlList.Create; 324 begin 325 inherited; 326 SubItems := THtmlElements.Create; 327 end; 328 329 destructor THtmlList.Destroy; 330 begin 331 FreeAndNil(SubItems); 332 inherited; 333 end; 334 335 { THtmlElements } 336 337 function THtmlElements.AddString(Text: string = ''): THtmlString; 338 begin 339 Result := THtmlString.Create; 340 Result.Text := Text; 341 Add(Result); 342 end; 343 344 function THtmlElements.AddBlock: THtmlBlock; 345 begin 346 Result := THtmlBlock.Create; 347 Add(Result); 348 end; 349 350 function THtmlElements.AddList: THtmlList; 351 begin 352 Result := THtmlList.Create; 353 Add(Result); 354 end; 355 356 function THtmlElements.AddInput: THtmlInput; 357 begin 358 Result := THtmlInput.Create; 359 Add(Result); 360 end; 361 300 362 { THtmlCell } 301 363 … … 319 381 destructor THtmlCell.Destroy; 320 382 begin 321 Value.Free;383 FreeAndNil(Value); 322 384 inherited; 323 385 end; … … 332 394 TXmlTag(Result).Name := 'tr'; 333 395 for Column := 0 to Cells.Count - 1 do 334 TXmlTag(Result).SubElements.Add New(THtmlCell(Cells[Column]).AsXmlElement);396 TXmlTag(Result).SubElements.Add(Cells[Column].AsXmlElement); 335 397 end; 336 398 337 399 constructor THtmlRow.Create; 338 400 begin 339 Cells := T ListObject.Create;401 Cells := THtmlCells.Create; 340 402 end; 341 403 342 404 destructor THtmlRow.Destroy; 343 405 begin 344 Cells.Free;406 FreeAndNil(Cells); 345 407 inherited; 346 408 end; … … 350 412 function THtmlTable.GetAsXmlElement: TXmlElement; 351 413 var 352 Row , Column: Integer;414 Row: Integer; 353 415 begin 354 416 Result := inherited; … … 356 418 Name := 'table'; 357 419 for Row := 0 to Rows.Count - 1 do 358 SubElements.Add New(THtmlRow(Rows[Row]).AsXmlElement);420 SubElements.Add(Rows[Row].AsXmlElement); 359 421 end; 360 422 end; … … 362 424 constructor THtmlTable.Create; 363 425 begin 364 Rows := T ListObject.Create;426 Rows := THtmlRows.Create; 365 427 end; 366 428 367 429 destructor THtmlTable.Destroy; 368 430 begin 369 Rows.Free;431 FreeAndNil(Rows); 370 432 inherited; 371 433 end; … … 448 510 destructor THtmlForm.Destroy; 449 511 begin 450 Action.Free;512 FreeAndNil(Action); 451 513 inherited; 452 514 end; … … 465 527 destructor THtmlDocument.Destroy; 466 528 begin 467 Body.Free;468 Styles.Free;469 Scripts.Free;529 FreeAndNil(Body); 530 FreeAndNil(Styles); 531 FreeAndNil(Scripts); 470 532 inherited; 471 533 end; … … 473 535 function THtmlDocument.GetAsXmlDocument: TXmlDocument; 474 536 var 475 DocType: TXMLTag;476 HTMLTag: TXMLTag;477 537 I: Integer; 478 538 begin 479 539 Result := TXmlDocument.Create; 480 540 with Result, Content do begin 481 DocType := TXMlTag.Create; 482 DocType.Name := '!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"'; 483 Doctype.EndTagSymbol := ''; 484 SubElements.Add(DocType); 485 HTMLTag := TXMLTag.Create; 486 with HTMLTag do begin 487 Name := 'html'; 488 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin 489 Name := 'head'; 490 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin 491 Name := 'title'; 492 with TXmlString(SubElements[SubElements.Add(TXmlString.Create)]) do begin 493 Text := Title; 494 end; 541 with SubElements.AddTag('!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"') do begin 542 EndTagSymbol := ''; 543 end; 544 with SubElements.AddTag('html') do begin 545 with SubElements.AddTag('head') do begin 546 with SubElements.AddTag('title') do begin 547 SubElements.AddString(Title); 495 548 end; 496 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin 497 Name := 'meta'; 549 with SubElements.AddTag('meta') do begin 498 550 Attributes.Add('http-equiv', 'Content-Language'); 499 551 Attributes.Add('content', ContentLanguage); 500 552 end; 501 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin 502 Name := 'meta'; 553 with SubElements.AddTag('meta') do begin 503 554 Attributes.Add('http-equiv', 'Content-Type'); 504 555 Attributes.Add('content', 'text/html; charset=' + ContentEncoding); 505 556 end; 506 557 for I := 0 to Styles.Count - 1 do 507 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin 508 Name := 'link'; 558 with SubElements.AddTag('link') do begin 509 559 Attributes.Add('rel', 'stylesheet'); 510 560 Attributes.Add('href', Styles[I]); … … 513 563 end; 514 564 for I := 0 to Scripts.Count - 1 do 515 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin 516 Name := 'script'; 565 with SubElements.AddTag('script') do begin 517 566 ShringEmpty := False; 518 567 Attributes.Add('type', 'text/javascript'); … … 520 569 end; 521 570 end; 522 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin 523 Name := 'body'; 571 with SubElements.AddTag('body') do begin 524 572 SubElements.Add(Body.AsXmlElement); 525 573 end; 526 574 end; 527 SubElements.Add(HTMLTag);528 575 end; 529 576 end; … … 534 581 begin 535 582 inherited; 536 SubItems := T ListObject.Create;583 SubItems := THtmlElements.Create; 537 584 end; 538 585 539 586 destructor THtmlBlock.Destroy; 540 587 begin 541 SubItems.Free;588 FreeAndNil(SubItems); 542 589 inherited; 543 590 end; … … 555 602 end; 556 603 for I := 0 to SubItems.Count - 1 do 557 SubElements.Add( THtmlElement(SubItems[I]).AsXmlElement);604 SubElements.Add(SubItems[I].AsXmlElement); 558 605 end; 559 606 end; … … 580 627 end; 581 628 582 { TIpAddress } 583 584 procedure TIpAddress.Assign(Source: TPersistent); 585 var 586 I: Integer; 587 begin 588 if Assigned(Source) then begin 589 if Source is TIpAddress then begin 590 for I := 0 to High(Octets) do 591 Octets[I] := TIpAddress(Source).Octets[I]; 592 end else inherited; 593 end else inherited; 594 end; 595 596 function TIpAddress.IsAddressString(Value: string): Boolean; 597 var 598 Parts: TListString; 599 begin 600 Result := True; 601 try 602 Parts := TListString.Create; 603 Parts.Explode(Value, '.', StrToStr); 604 if Parts.Count = 4 then begin 605 if (StrToInt(Parts[3]) < 0) or (StrToInt(Parts[3]) > 255) then Result := False; 606 if (StrToInt(Parts[2]) < 0) or (StrToInt(Parts[2]) > 255) then Result := False; 607 if (StrToInt(Parts[1]) < 0) or (StrToInt(Parts[1]) > 255) then Result := False; 608 if (StrToInt(Parts[0]) < 0) or (StrToInt(Parts[0]) > 255) then Result := False; 609 end else Result := False; 610 finally 611 Parts.Free; 612 end; 613 end; 614 615 function TIpAddress.GetAddrClass: TAddrClass; 616 begin 617 if (Octets[3] and $80) = 0 then Result := acA 618 else begin 619 if (Octets[3] and $40) = 0 then Result := acB 620 else begin 621 if (Octets[3] and $20) = 0 then Result := acC 622 else begin 623 if (Octets[3] and $10) = 0 then Result := acD 624 else Result := acE; 625 end; 626 end; 627 end; 628 end; 629 630 function TIpAddress.GetAsCardinal: Cardinal; 631 begin 632 Result := Octets[0] or (Octets[1] shl 8) or (Octets[2] shl 16) or (Octets[3] shl 24); 633 end; 634 635 function TIpAddress.GetAsString: string; 636 begin 637 Result := IntToStr(Octets[3]) + '.' + IntToStr(Octets[2]) + '.' + 638 IntToStr(Octets[1]) + '.' + IntToStr(Octets[0]); 639 end; 640 641 function TIpAddress.GetBroadcast: Boolean; 642 begin 643 Result := AsCardinal = High(Cardinal); 644 end; 645 646 procedure TIpAddress.SetAsCardinal(const Value: Cardinal); 647 begin 648 Octets[0] := Byte(Value); 649 Octets[1] := Byte(Value shr 8); 650 Octets[2] := Byte(Value shr 16); 651 Octets[3] := Byte(Value shr 24); 652 end; 653 654 procedure TIpAddress.SetAsString(const Value: string); 655 var 656 Parts: TListString; 657 begin 658 try 659 Parts := TListString.Create; 660 Parts.Explode(Value, '.', StrToStr); 661 try 662 // if Length(Parts) = 4 then begin 663 Octets[0] := StrToInt(Parts[3]); 664 Octets[1] := StrToInt(Parts[2]); 665 Octets[2] := StrToInt(Parts[1]); 666 Octets[3] := StrToInt(Parts[0]); 667 // end else raise EConvertError.Create('String to IP address conversion error'); 668 except 669 raise EConvertError.Create(SStringToIPConversionError); 670 end; 671 finally 672 Parts.Free; 673 end; 674 end; 675 676 procedure TIpAddress.SetBroadcast(const Value: Boolean); 677 begin 678 AsCardinal := High(Cardinal); 679 end; 629 { TURL } 680 630 681 631 constructor TURL.Create; … … 686 636 destructor TURL.Destroy; 687 637 begin 688 Host.Free;638 FreeAndNil(Host); 689 639 inherited; 690 640 end; … … 724 674 end; 725 675 726 727 { TDomainAddress }728 729 function TDomainAddress.GetAsString: string;730 begin731 try732 Levels.Reverse;733 Result := Levels.Implode('.', StrToStr);734 finally735 Levels.Reverse;736 end;737 end;738 739 procedure TDomainAddress.SetAsString(const Value: string);740 begin741 Levels.Explode(Value, '.', StrToStr);742 Levels.Reverse;743 end;744 745 constructor TDomainAddress.Create;746 begin747 Levels := TListString.Create;748 end;749 750 destructor TDomainAddress.Destroy;751 begin752 Levels.Free;753 inherited;754 end;755 756 676 { THtmlLink } 757 677 … … 763 683 destructor THtmlLink.Destroy; 764 684 begin 765 Target.Free;685 FreeAndNil(Target); 766 686 inherited; 767 687 end; … … 791 711 end; 792 712 793 { THostAddress }794 795 constructor THostAddress.Create;796 begin797 DomainName := TDomainAddress.Create;798 IpAddress := TIpAddress.Create;799 State := asDomainName;800 DomainName.AsString := 'localhost';801 end;802 803 destructor THostAddress.Destroy;804 begin805 DomainName.Free;806 IpAddress.Free;807 inherited;808 end;809 810 function THostAddress.GetAsString: string;811 begin812 case State of813 asDomainName: Result := DomainName.AsString;814 asIpAddress: Result := IpAddress.AsString;815 end;816 end;817 818 procedure THostAddress.SetAsString(const Value: string);819 begin820 if IpAddress.IsAddressString(Value) then begin821 State := asIpAddress;822 IpAddress.AsString := Value;823 end else begin824 State := asDomainName;825 DomainName.AsString := Value;826 end;827 end;828 829 713 { THtmlImage } 830 714 … … 836 720 destructor THtmlImage.Destroy; 837 721 begin 838 Source.Free;722 FreeAndNil(Source); 839 723 inherited; 840 724 end; … … 889 773 destructor TQueryString.Destroy; 890 774 begin 891 Data.Free;775 FreeAndNil(Data); 892 776 inherited; 893 777 end; -
trunk/Packages/CoolWeb/Common/UXmlClasses.pas
r137 r138 4 4 5 5 uses 6 Classes, SysUtils, StrUtils, SpecializedList, SpecializedDictionary;6 Classes, SysUtils, StrUtils, Generics.Collections, UGenerics; 7 7 8 8 type … … 12 12 public 13 13 property AsString: string read GetAsString; 14 end; 15 16 TXmlTag = class; 17 TXmlString = class; 18 19 { TXmlElements } 20 21 TXmlElements = class(TObjectList<TXmlElement>) 22 function AddTag(Name: string): TXmlTag; 23 function AddString(Text: string): TXmlString; 14 24 end; 15 25 … … 29 39 Name: string; 30 40 Attributes: TDictionaryStringString; 31 SubElements: T ListObject; // TListObject<TXmlElement>;41 SubElements: TXmlElements; 32 42 constructor Create; 33 43 destructor Destroy; override; … … 52 62 implementation 53 63 64 { TXmlElements } 65 66 function TXmlElements.AddTag(Name: string): TXmlTag; 67 begin 68 Result := TXmlTag.Create; 69 Result.Name := Name; 70 Add(Result); 71 end; 72 73 function TXmlElements.AddString(Text: string): TXmlString; 74 begin 75 Result := TXmlString.Create; 76 Result.Text := Text; 77 Add(Result); 78 end; 79 54 80 { THtmlElement } 55 81 … … 58 84 ShringEmpty := True; 59 85 Attributes := TDictionaryStringString.Create; 60 SubElements := T ListObject.Create;86 SubElements := TXmlElements.Create; 61 87 EndTagSymbol := '/'; 62 88 end; … … 64 90 destructor TXmlTag.Destroy; 65 91 begin 66 Attributes.Free;67 SubElements.Free;92 FreeAndNil(Attributes); 93 FreeAndNil(SubElements); 68 94 inherited; 69 95 end; … … 74 100 I: Integer; 75 101 Content: string; 102 Attribute: TPair<string, string>; 76 103 begin 77 104 Content := ''; … … 80 107 81 108 AttributesText := ''; 82 for I := 0 to Attributes.Count - 1do83 AttributesText := AttributesText + ' ' + Attribute s.Keys[I] + '="' + Attributes[I].Value + '"';109 for Attribute in Attributes do 110 AttributesText := AttributesText + ' ' + Attribute.Key + '="' + Attribute.Value + '"'; 84 111 85 112 if Name <> '' then begin … … 123 150 destructor TXmlDocument.Destroy; 124 151 begin 125 Content.Free;126 MainTag.Free;152 FreeAndNil(Content); 153 FreeAndNil(MainTag); 127 154 inherited; 128 155 end; -
trunk/Packages/CoolWeb/CoolWeb.lpk
r132 r138 40 40 <License Value="GNU/GPL"/> 41 41 <Version Minor="3"/> 42 <Files Count="1 8">42 <Files Count="19"> 43 43 <Item1> 44 44 <Filename Value="WebServer/UHTTPServer.pas"/> … … 121 121 <UnitName Value="UWebUser"/> 122 122 </Item18> 123 <Item19> 124 <Filename Value="Common/UIpAddress.pas"/> 125 <UnitName Value="UIpAddress"/> 126 </Item19> 123 127 </Files> 124 128 <CompatibilityMode Value="True"/> 125 <RequiredPkgs Count=" 8">129 <RequiredPkgs Count="7"> 126 130 <Item1> 127 131 <PackageName Value="TurboPowerIProDsgn"/> … … 140 144 </Item5> 141 145 <Item6> 142 <PackageName Value="TemplateGenerics"/> 143 <MaxVersion Minor="3"/> 144 <MinVersion Minor="3" Valid="True"/> 146 <PackageName Value="synapse"/> 145 147 </Item6> 146 148 <Item7> 147 <PackageName Value="synapse"/>148 </Item7>149 <Item8>150 149 <PackageName Value="FCL"/> 151 150 <MinVersion Major="1" Valid="True"/> 152 </Item 8>151 </Item7> 153 152 </RequiredPkgs> 154 153 <UsageOptions> -
trunk/Packages/CoolWeb/CoolWeb.pas
r115 r138 5 5 unit CoolWeb; 6 6 7 {$warn 5023 off : no warning about unused units} 7 8 interface 8 9 … … 11 12 UTurboPowerForm, UHTTPSessionFile, UHTTPSessionMySQL, USqlDatabase, 12 13 UTCPServer, UPageList, UHtmlClasses, UMemoryStreamEx, UMIMEType, 13 UXmlClasses, UWebPage, UWebApp, LazIDEReg, UWebUser, LazarusPackageIntf; 14 UXmlClasses, UWebPage, UWebApp, LazIDEReg, UWebUser, UIpAddress, 15 LazarusPackageIntf; 14 16 15 17 implementation -
trunk/Packages/CoolWeb/Modules/UPageList.pas
r137 r138 7 7 8 8 type 9 10 { TPageList } 11 9 12 TPageList = class 10 13 TotalCount: Integer; … … 19 22 Around: Integer; 20 23 constructor Create; 24 destructor Destroy; override; 21 25 function Hyperlink: string; 22 26 function Process: string; … … 33 37 QueryItems.SetStringServer; 34 38 HTMLId := ''; 39 end; 40 41 destructor TPageList.Destroy; 42 begin 43 FreeAndNil(QueryItems); 44 inherited; 35 45 end; 36 46 -
trunk/Packages/CoolWeb/Modules/UWebUser.pas
r137 r138 5 5 uses 6 6 Classes, SysUtils, synacode, USqlDatabase, UCommon, UHTTPServer, 7 SpecializedDictionary;7 UGenerics; 8 8 9 9 const … … 60 60 DbRows: TDbRows; 61 61 Id: Integer; 62 begin 63 try 64 DbRows := TDbRows.Create; 65 if HandlerData.Request.Cookies.SearchKey('SessionId') <> -1 then begin 62 Value: string; 63 begin 64 try 65 DbRows := TDbRows.Create; 66 if HandlerData.Request.Cookies.TryGetValue('SessionId', Value) then begin 66 67 Database.Query(DbRows, 'SELECT * FROM `UserOnline` WHERE `SessionId`="' + 67 HandlerData.Request.Cookies.Values['SessionId']+ '"');68 Value + '"'); 68 69 if DbRows.Count > 0 then begin 69 70 // Update exited 70 Id := StrToInt(DbRows[0]. Values['Id']);71 User := StrToInt(DbRows[0]. Values['User']);71 Id := StrToInt(DbRows[0].Items['Id']); 72 User := StrToInt(DbRows[0].Items['User']); 72 73 Database.Query(DbRows, 'UPDATE `UserOnline` SET `ActivityTime` = NOW() WHERE `Id`=' + IntToStr(Id)); 73 74 end else begin 74 75 // Create new record 75 76 Database.Query(DbRows, 'INSERT INTO `UserOnline` (`User`, `ActivityTime`, `SessionId`, `ScriptName`) ' + 76 'VALUES (1, NOW(), "' + HandlerData.Request.Cookies.Values['SessionId']+ '", "")');77 'VALUES (1, NOW(), "' + Value + '", "")'); 77 78 Id := Database.LastInsertId; 78 79 User := 1; … … 88 89 var 89 90 DbRows: TDbRows; 91 SessionId: string; 90 92 begin 91 93 Logout; 94 if HandlerData.Request.Cookies.TryGetValue('SessionId', SessionId) then 92 95 try 93 96 DbRows := TDbRows.Create; 94 97 Database.Query(DbRows, 'UPDATE `UserOnline` SET `User` = ' + IntToStr(User) + ', `LoginTime` = NOW() WHERE `SessionId`="' + 95 HandlerData.Request.Cookies.Values['SessionId']+ '"');98 SessionId + '"'); 96 99 finally 97 100 DbRows.Free; … … 103 106 var 104 107 DbRows: TDbRows; 108 SessionId: string; 105 109 begin 106 110 if Id = AnonymousUserId then Update; 107 if User <> AnonymousUserId then begin 111 if (User <> AnonymousUserId) and 112 HandlerData.Request.Cookies.TryGetValue('SessionId', SessionId) then begin 108 113 try 109 114 DbRows := TDbRows.Create; 110 115 Database.Query(DbRows, 'UPDATE `UserOnline` SET `User` = ' + IntToStr(AnonymousUserId) + ' WHERE `SessionId`="' + 111 HandlerData.Request.Cookies.Values['SessionId']+ '"');116 SessionId + '"'); 112 117 finally 113 118 DbRows.Free; … … 178 183 DbRows := TDbRows.Create; 179 184 Database.Query(DbRows, 'SELECT `Id` FROM `User` WHERE `Name`="' + Name + '"'); 180 if DbRows.Count = 1 then Result := StrToInt(DbRows[0].Items[ 0].Value)185 if DbRows.Count = 1 then Result := StrToInt(DbRows[0].Items['Id']) 181 186 else Result := -1; 182 187 finally … … 193 198 Database.Query(DbRows, 'SELECT `Id` FROM `User` WHERE `Name`="' + Name + '" AND ' + 194 199 '`Password` = SHA1(CONCAT("' + Password + '", Salt))'); 195 if DbRows.Count = 1 then Result := StrToInt(DbRows[0].Items[ 0].Value)200 if DbRows.Count = 1 then Result := StrToInt(DbRows[0].Items['Id']) 196 201 else Result := -1; 197 202 finally … … 208 213 Database.Query(DbRows, 'SELECT * FROM `User` WHERE `Id`="' + IntToStr(Id) + '"'); 209 214 if DbRows.Count = 1 then begin 210 Name := DbRows[0]. Values['Name'];211 FullName := DbRows[0]. Values['FullName'];212 Email := DbRows[0]. Values['Email'];215 Name := DbRows[0].Items['Name']; 216 FullName := DbRows[0].Items['FullName']; 217 Email := DbRows[0].Items['Email']; 213 218 end; // else raise ENotFound.Create(Format(SUserNotFound, [IntToStr(Id)])); 214 219 finally … … 233 238 try 234 239 DbRows2 := TDbRows.Create; 235 OperationId := StrToInt(DbRows[0]. Values['Id']);240 OperationId := StrToInt(DbRows[0].Items['Id']); 236 241 237 242 // Check user-operation relation … … 247 252 '`User` = ' + IntToStr(Id) + ' AND `AssignedGroup` IS NOT NULL'); 248 253 if DbRows2.Count > 0 then begin 249 if CheckGroupPermission(StrToInt(DbRows2[0]. Values['AssignedGroup']), OperationId) then begin254 if CheckGroupPermission(StrToInt(DbRows2[0].Items['AssignedGroup']), OperationId) then begin 250 255 Result := True; 251 256 Exit; … … 280 285 '`User` = ' + IntToStr(Id) + ' AND `AssignedGroup` IS NOT NULL'); 281 286 if DbRows2.Count > 0 then begin 282 if CheckGroupPermission(StrToInt(DbRows2[0]. Values['AssignedGroup']), Operation) then begin287 if CheckGroupPermission(StrToInt(DbRows2[0].Items['AssignedGroup']), Operation) then begin 283 288 Result := True; 284 289 Exit; -
trunk/Packages/CoolWeb/Persistence/USqlDatabase.pas
r137 r138 6 6 7 7 uses 8 SysUtils, Classes, Dialogs, mysql50, TypInfo, SpecializedDictionary, 9 SpecializedList; 8 SysUtils, Classes, Dialogs, mysql50, TypInfo, UGenerics, Generics.Collections; 10 9 11 10 type … … 21 20 TLogEvent = procedure(Sender: TObject; Text: string) of object; 22 21 23 TDbRows = class(TList Object)22 TDbRows = class(TList<TDictionaryStringString>) 24 23 private 25 24 function GetData(Index: Integer): TDictionaryStringString; … … 155 154 TimeParts := TListString.Create; 156 155 157 Parts.Explode( Value, ' ', StrToStr);158 DateParts.Explode( Parts[0], '-', StrToStr);156 Parts.Explode(' ', Value); 157 DateParts.Explode('-', Parts[0]); 159 158 Result := EncodeDate(StrToInt(DateParts[0]), StrToInt(DateParts[1]), 160 159 StrToInt(DateParts[2])); 161 160 if Parts.Count > 1 then begin 162 TimeParts.Explode( Parts[1], ':', StrToStr);161 TimeParts.Explode(':', Parts[1]); 163 162 Result := Result + EncodeTime(StrToInt(TimeParts[0]), StrToInt(TimeParts[1]), 164 163 StrToInt(TimeParts[2]), 0); … … 210 209 Value: string; 211 210 DbResult: TDbRows; 211 Item: TPair<string, string>; 212 212 begin 213 213 LastUsedTable := ATable; 214 214 DbNames := ''; 215 215 DbValues := ''; 216 for I := 0 to Data.Count - 1do begin217 Value := Data.Items[I].Value;216 for Item in Data do begin 217 Value := Item.Value; 218 218 StringReplace(Value, '"', '\"', [rfReplaceAll]); 219 219 if Value = 'NOW()' then DbValues := DbValues + ',' + Value 220 220 else DbValues := DbValues + ',"' + Value + '"'; 221 DbNames := DbNames + ',`' + Data.Keys[I]+ '`';221 DbNames := DbNames + ',`' + Item.Key + '`'; 222 222 end; 223 223 System.Delete(DbNames, 1, 1); … … 274 274 I: Integer; 275 275 DbResult: TDbRows; 276 Item: TPair<string, string>; 276 277 begin 277 278 LastUsedTable := ATable; 278 279 DbNames := ''; 279 280 DbValues := ''; 280 for I := 0 to Data.Count - 1do begin281 Value := Data.Items[I].Value;281 for Item in Data do begin 282 Value := Item.Value; 282 283 StringReplace(Value, '"', '\"', [rfReplaceAll]); 283 284 if Value = 'NOW()' then DbValues := DbValues + ',' + Value 284 285 else DbValues := DbValues + ',"' + Value + '"'; 285 DbNames := DbNames + ',`' + Data.Keys[I]+ '`';286 DbNames := DbNames + ',`' + Item.Key + '`'; 286 287 end; 287 288 System.Delete(DbNames, 1, 1); … … 314 315 I: Integer; 315 316 DbResult: TDbRows; 317 Item: TPair<string, string>; 316 318 begin 317 319 LastUsedTable := ATable; 318 320 DbValues := ''; 319 for I := 0 to Data.Count - 1do begin320 Value := Data.Items[I].Value;321 for Item in Data do begin 322 Value := Item.Value; 321 323 StringReplace(Value, '"', '\"', [rfReplaceAll]); 322 324 if Value = 'NOW()' then DbValues := DbValues + ',' + Value 323 else DbValues := DbValues + ',`' + Data.Keys[I]+ '` =' + '"' + Value + '"';325 else DbValues := DbValues + ',`' + Item.Key + '` =' + '"' + Value + '"'; 324 326 end; 325 327 System.Delete(DbValues, 1, 1); -
trunk/Packages/CoolWeb/WebServer/UHTTPServer.pas
r137 r138 4 4 5 5 uses 6 Classes, SysUtils, UCommon, UMemoryStreamEx, UMIMEType, 7 Synautil, SpecializedList, SpecializedDictionary, Syncobjs;6 Classes, SysUtils, UCommon, UMemoryStreamEx, UMIMEType, Synautil, Syncobjs, 7 Generics.Collections, UGenerics; 8 8 9 9 type … … 75 75 { TRequestHandlerList } 76 76 77 TRequestHandlerList = class(T ListObject)77 TRequestHandlerList = class(TObjectList<TRequestHandler>) 78 78 procedure Add(AName: string; AHandler: TRequestEvent); 79 79 function IndexOfName(AName: string): TRequestHandler; … … 131 131 var 132 132 I: Integer; 133 Item: TPair<string, string>; 133 134 begin 134 135 with HandlerData, Response.Content do begin … … 145 146 146 147 WriteString('<h5>Request HTTP headers</h5>'); 147 for I := 0 to Request.Headers.Count - 1 do begin; 148 with Request.Headers.Items[I] do 149 WriteString(Key + ': ' + Value + '<br/>'); 148 for Item in Request.Headers do begin; 149 WriteString(Item.Key + ': ' + Item.Value + '<br/>'); 150 150 end; 151 151 152 152 WriteString('<h5>Request HTTP GET</h5>'); 153 for I := 0 to Request.Query.Count - 1 do begin 154 with Request.Query.Items[I] do 155 WriteString(Key + ': ' + Value + '<br/>'); 153 for Item in Request.Query do begin 154 WriteString(Item.Key + ': ' + Item.Value + '<br/>'); 156 155 end; 157 156 158 157 WriteString('<h5>Request HTTP cookies</h5>'); 159 158 for I := 0 to Request.Cookies.Count - 1 do begin 160 with Request.Cookies.Items[I] do 161 WriteString(Key + ': ' + Value + '<br/>'); 159 WriteString(Item.Key + ': ' + Item.Value + '<br/>'); 162 160 end; 163 161 … … 170 168 171 169 WriteString('<h5>Request HTTP POST</h5>'); 172 for I := 0 to Request.Post.Count - 1 do begin 173 with Request.Post.Items[I] do 174 WriteString(Key + ': ' + Value + '<br/>'); 170 for Item in Request.Post do begin 171 WriteString(Item.Key + ': ' + Item.Value + '<br/>'); 175 172 end; 176 173 … … 181 178 WriteString('<h5>Response HTTP headers</h5>'); 182 179 with Response.Content do 183 for I := 0 to Response.Headers.Count - 1 do begin 184 with Response.Headers.Items[I] do 185 WriteString(Key + ': ' + Value + '<br/>'); 180 for Item in Response.Headers do begin 181 WriteString(Item.Key + ': ' + Item.Value + '<br/>'); 186 182 end; 187 183 188 184 WriteString('<h5>Response HTTP cookies</h5>'); 189 for I := 0 to Response.Cookies.Count - 1 do begin; 190 with Response.Cookies.Items[I] do 191 WriteString(Key + ': ' + Value + '<br/>'); 185 for Item in Response.Cookies do begin; 186 WriteString(Item.Key + ': ' + Item.Value + '<br/>'); 192 187 end; 193 188 end; … … 197 192 begin 198 193 with HandlerData, Response.Content do begin 199 WriteString('<html><body>' + Format(SPageNotFound, [ Request.Path.Implode('/', StrToStr)]) + '</body></html>');194 WriteString('<html><body>' + Format(SPageNotFound, [Implode('/', Request.Path)]) + '</body></html>'); 200 195 end; 201 196 end; … … 218 213 begin 219 214 with HandlerData do begin 220 FileName := DocumentRoot + DirectorySeparator + Request.Path.Implode('/', StrToStr);215 FileName := DocumentRoot + DirectorySeparator + Implode('/', Request.Path); 221 216 if FileExists(FileName) then begin 222 Response.Headers. Values['Content-Type'] := GetMIMEType(Copy(ExtractFileExt(FileName), 2, 255));217 Response.Headers.Items['Content-Type'] := GetMIMEType(Copy(ExtractFileExt(FileName), 2, 255)); 223 218 try 224 219 BinaryFile := TFileStream.Create(FileName, fmOpenRead); … … 230 225 with Response.Content do begin 231 226 //WriteLn(Format(SFileNotFound, [Request.Path.Implode('/', StrToStr)])); 232 WriteString('<html><body>' + Format(SFileNotFound, [ Request.Path.Implode('/', StrToStr)]) + '</body></html>');227 WriteString('<html><body>' + Format(SFileNotFound, [Implode('/', Request.Path)]) + '</body></html>'); 233 228 end; 234 229 end; … … 357 352 Pair := TListString.Create; 358 353 Clear; 359 Parts.Explode( Text, '&', StrToStr);354 Parts.Explode('&', Text); 360 355 for I := 0 to Parts.Count - 1 do begin 361 Pair.Explode( Parts[I], '=', StrToStr);356 Pair.Explode('=', Parts[I]); 362 357 if Pair.Count >= 2 then 363 358 Add(Pair[0], Pair[1]); … … 371 366 function TQueryParameterList.Syntetize: string; 372 367 var 373 I : Integer;368 Item: TPair<string, string>; 374 369 begin 375 370 Result := ''; 376 for I := 0 to Count - 1do377 Result := Result + '&' + Keys[I] + '=' + Items[I].Value;371 for Item in Self do 372 Result := Result + '&' + Item.Key + '=' + Item.Value; 378 373 Result := Copy(Result, 6, Length(Result)); 379 374 end; … … 391 386 Pair := TListString.Create; 392 387 Clear; 393 Parts.Explode( Text, ';', StrToStr);388 Parts.Explode(';', Text); 394 389 for I := 0 to Parts.Count - 1 do begin 395 Pair.Explode( Parts[I], '=', StrToStr);390 Pair.Explode('=', Parts[I]); 396 391 if Pair.Count >= 2 then 397 392 Add(Trim(Pair[0]), Trim(Pair[1])); … … 405 400 function TCookieList.Syntetize: string; 406 401 var 407 I : Integer;402 Item: TPair<string, string>; 408 403 begin 409 404 Result := ''; 410 for I := 0 to Count - 1do411 Result := Result + '; ' + Keys[I] + '=' + Items[I].Value;405 for Item in Self do 406 Result := Result + '; ' + Item.Key + '=' + Item.Value; 412 407 Result := Copy(Result, 2, Length(Result)); 413 408 end; -
trunk/Packages/CoolWeb/WebServer/UHTTPServerCGI.pas
r137 r138 4 4 5 5 uses 6 Classes, SysUtils, UHTTPServer, SpecializedList, IOStream;6 Classes, SysUtils, UHTTPServer, IOStream, Generics.Collections; 7 7 8 8 type … … 54 54 Buffer: string; 55 55 Count: Integer; 56 Item: TPair<string, string>; 56 57 begin 57 58 HandlerData := THTTPHandlerData.Create; … … 96 97 EnvVars.Values['QUERY_STRING'] := Copy(EnvVars.Values['QUERY_STRING'], 1, 97 98 Length(EnvVars.Values['QUERY_STRING']) - 1); 98 Request.Path.Explode( EnvVars.Values['QUERY_STRING'], '/', StrToStr);99 Request.Path.Explode('/', EnvVars.Values['QUERY_STRING']); 99 100 if Pos('?', EnvVars.Values['REQUEST_URI']) > 0 then begin 100 101 Request.Query.Parse(Copy(EnvVars.Values['REQUEST_URI'], … … 129 130 with Response do begin 130 131 // Generate cookies 131 for I := 0 to Cookies.Count - 1do132 Headers.Add('Set-Cookie', Cookies.Keys[I] + '=' + Cookies.Items[I].Value);132 for Item in Cookies do 133 Headers.Add('Set-Cookie', Item.Key + '=' + Item.Value); 133 134 // + ';path=/;expires=' + RFC822DateTime(Now); 134 135 135 136 // Generate headers 136 for I := 0 to Headers.Count - 1do begin137 WriteLn( Headers.Keys[I] + ': ' + Headers.Items[I].Value);137 for Item in Headers do begin 138 WriteLn(Item.Key + ': ' + Item.Value); 138 139 end; 139 140 -
trunk/Packages/CoolWeb/WebServer/UHTTPServerTCP.pas
r137 r138 4 4 5 5 uses 6 Classes, SysUtils, UHTTPServer, UTCPServer, SpecializedList, SynaUtil; 6 Classes, SysUtils, UHTTPServer, UTCPServer, SynaUtil, Generics.Collections, 7 UGenerics; 7 8 8 9 type … … 42 43 I: Integer; 43 44 ContentLength: Integer; 45 Value: string; 46 Item: TPair<string, string>; 44 47 begin 45 48 with TTCPClientThread(Sender), Socket do begin … … 60 63 WriteLn(IntToStr(Id) + ' ' + Line); 61 64 if (LineIndex = 0) then begin 62 LineParts.Explode( Line, ' ', StrToStr);65 LineParts.Explode(' ', Line); 63 66 if (LineParts.Count >= 3) then begin 64 67 Request.Method := LineParts[0]; 65 68 if Pos('?', LineParts[1]) > 0 then begin 66 69 Request.Query.Parse(Copy(LineParts[1], Pos('?', LineParts[1]) + 1, Length(LineParts[1]))); 67 Request.Path.Explode( Copy(LineParts[1], 1, Pos('?', LineParts[1]) - 1), '/', StrToStr);70 Request.Path.Explode('/', Copy(LineParts[1], 1, Pos('?', LineParts[1]) - 1)); 68 71 end else begin 69 Request.Path.Explode( LineParts[1], '/', StrToStr);72 Request.Path.Explode('/', LineParts[1]); 70 73 Request.Query.Clear; 71 74 end; … … 75 78 end; 76 79 end else begin 77 LineParts.Explode( Line, ' ', StrToStr, 2);80 LineParts.Explode(' ', Line, 2); 78 81 if (LineParts.Count = 2) and (LineParts[0][Length(LineParts[0])] = ':') then begin 79 82 LineParts[0] := Copy(LineParts[0], 1, Length(LineParts[0]) - 1); 80 83 Request.Headers.Add(LineParts[0], LineParts[1]); 81 //WriteLn(Line);82 84 end; 83 85 end; … … 86 88 87 89 if Request.Method = 'POST' then begin 88 ContentLength := StrToInt(Request.Headers.Values['Content-Length']); 90 if Request.Headers.TryGetValue('Content-Length', Value) then 91 ContentLength := StrToInt(Value); 89 92 SetLength(Line, ContentLength); 90 93 RecvBufferEx(PByte(Line), ContentLength, 1000); … … 96 99 97 100 // Process cookies 98 if Request.Headers. SearchKey('Cookie') <> -1then99 Request.Cookies.Parse( Request.Headers.Values['Cookie']);101 if Request.Headers.TryGetValue('Cookie', Value) then 102 Request.Cookies.Parse(Value); 100 103 101 104 // Load session variables … … 120 123 121 124 // Handle cookies 122 for I := 0 to Cookies.Count - 1do123 Headers.Add('Set-Cookie', Cookies.Keys[I] + '=' + Cookies.Items[I].Value);125 for Item in Cookies do 126 Headers.Add('Set-Cookie', Item.Key + '=' + Item.Value); 124 127 // + ';path=/;expires=' + RFC822DateTime(Now); 125 128 126 129 // Send headers 127 for I := 0 to Headers.Count - 1do begin130 for Item in Headers do begin 128 131 //WriteLn(Headers.Keys[I] + ': ' + Headers.Items[I].Value + #13#10); 129 SendString( Headers.Keys[I] + ': ' + Headers.Items[I].Value + #13#10);132 SendString(Item.Key + ': ' + Item.Value + #13#10); 130 133 end; 131 134 SendString(#13#10); -
trunk/Packages/CoolWeb/WebServer/UHTTPSessionFile.pas
r137 r138 55 55 56 56 procedure THTTPSessionStorageFile.GetSessionId(HandlerData: THTTPHandlerData); 57 var 58 Value: string; 57 59 begin 58 60 with HandlerData do begin 59 if Request.Cookies. SearchKey(SessionIdCookieName) <> -1then begin60 SessionId := Request.Cookies.Values[SessionIdCookieName];61 if Request.Cookies.TryGetValue(SessionIdCookieName, Value) then begin 62 SessionId := Value; 61 63 end else begin 62 64 SessionId := GetNewSessionId; 63 Response.Cookies. Values[SessionIdCookieName] := SessionId;65 Response.Cookies.Items[SessionIdCookieName] := SessionId; 64 66 end; 65 67 end; … … 96 98 end else raise Exception.Create(SCantCreateSessionStorageDirectory); 97 99 98 HandlerData.Response.Cookies. Values[SessionIdCookieName] := HandlerData.SessionId;100 HandlerData.Response.Cookies.Items[SessionIdCookieName] := HandlerData.SessionId; 99 101 finally 100 102 Lock.Release; -
trunk/Packages/CoolWeb/WebServer/UHTTPSessionMySQL.pas
r137 r138 64 64 65 65 procedure THTTPSessionStorageMySQL.GetSessionId(HandlerData: THTTPHandlerData); 66 var 67 Value: string; 66 68 begin 67 69 with HandlerData do begin 68 if Request.Cookies. SearchKey(SessionIdCookieName) <> -1then begin69 SessionId := Request.Cookies.Values[SessionIdCookieName];70 if Request.Cookies.TryGetValue(SessionIdCookieName, Value) then begin 71 SessionId := Value; 70 72 end else begin 71 73 SessionId := GetNewSessionId; … … 87 89 HandlerData.SessionId + '"'); 88 90 if DbRows.Count > 0 then begin 89 HandlerData.Session.Text := DbRows[0]. Values['Variables'];91 HandlerData.Session.Text := DbRows[0].Items['Variables']; 90 92 end else begin 91 93 HandlerData.SessionId := GetNewSessionId; … … 114 116 else Database.Query(DbRows2, 'INSERT INTO `HTTPSession` (`Time`, `Variables`, `Identification`) VALUES (' + 115 117 'NOW(), "' + HandlerData.Session.Text + '", "' + HandlerData.SessionId + '")'); 116 HandlerData.Response.Cookies. Values[SessionIdCookieName] := HandlerData.SessionId;118 HandlerData.Response.Cookies.Items[SessionIdCookieName] := HandlerData.SessionId; 117 119 finally 118 120 DbRows2.Free;
Note:
See TracChangeset
for help on using the changeset viewer.