WebLaz corrections lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- edgarrod71 · 2023-03-19 19:59 Despite of life and economic problems I had last years I decided to come back to the programming world, tried to ask for a job and market prefers all kind of languages derived from C++ so I decided to learn more than I had in my head.  But let’s be honest, nothing as powerful and easier to learn than Pascal… So keeping on the dilemma. After translating the fpweb Tutorial from Leledumbo into Spanish, I tried to follow the instructions point by point, and had some issues I’d like to share with you. First, I clicked on Project / New Project / and selected HTTP Server Application, then selected all options on the dialog box, Register Location to serve files from, Location (let’s change this caption to another more “new user” specific friendly like “Location on URL”), Directory (Must change to “Physical location of your files, leave empty if you plan to serve your files on the same place the web server application is”), Port (nothing to change), and the thread for server request, after this dialog, I tried to run it, went to a browser and nothing happens…  Turns that it doesn’t run because the cthreads unit is not put on the .lpr file. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   {$IFDEF UNIX}{$IFDEF UseCThreads}  cthreads,  {$ENDIF}{$ENDIF} Next issue is the first procedure RegisterFileLocation, digging the code, it’s not working according the logic of instructions there.  Lines 65 and 71 are put wrong, must check if the aDirectory variable is empty and then check if that directory exists.  So lines 65 and 71 ought to work together, at the end, the procedure is more to work like:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure RegisterFileLocation(Const ALocation, ADirectory: String);begin  if ALocation = '' then    Raise HTTPError.Create(SErrNoLocation);   if Pos('/',ALocation)<>0 then    Raise HTTPError.Create(SErrInvalidLocation);  if Locations = Nil then    Locations:=TStringList.Create;  if ADirectory = '' then    Locations.Values[IncludeHTTPPathDelimiter(ALocation)] := ExtractFilePath(ParamStr(0))  else    if DirectoryExists(ADirectory) then      Locations.Values[IncludeHTTPPathDelimiter(ALocation)] := IncludeTrailingPathDelimiter(ADirectory)    else      Raise HTTPError.Create(SErrInvalidDirectory);  if DefaultFileModuleClass = Nil then    DefaultFileModuleClass := TFPCustomFileModule;  RegisterHTTPModule(ALocation, DefaultFileModuleClass, true);end; Then comes the 232 error… turns that omitting {$IFDEF UseCThreads} above, it runs well. There’s a lot more… but this is a good start. ---------------------------------------------------------------------- egsuh · 2023-03-20 01:34 Welcome to the world of FPC web server. I remember I've read somewhere that "registerfilelocation" does not work in Windows system. What operating system are you using? ---------------------------------------------------------------------- edgarrod71 · 2023-03-21 13:54 Hi egsuh, thanks for your welcome, I was Apexcol before, but because I couldn't get my apexcol@gmail.com I had to be now Edgarrod71. There are a lot of things to fix, I'll take a look on that as soon as I can, but, can you help me to find the unit where it is and the line? I'm using now IOS El Capitan 10.11, using also Lazarus 2.0.12 with ppcx_64 compiler and fpc 3.0.4. ---------------------------------------------------------------------- edgarrod71 · 2023-03-22 21:54 Lol... I fixed it!  Must work on every platform ---------------------------------------------------------------------- egsuh · 2023-03-23 06:22 Wow. That's great. But Lazarus 2.2.6 with FPC 3.2.2 is available. The problems may have been fixed in the new version. Why are you keeping older version? ---------------------------------------------------------------------- edgarrod71 · 2023-03-24 04:17 Because using the downloadable versions, I cannot use the caret '^' nor any of foreign characters inside strings like 'ñáéíóúÁÉÍÓÚÜü', and the only version I could install on my Macbook pro using El Capitan is 2.0.12.  By the way I have in other partition Big Sur, and there I could install latest version but with the problem described...  I posted this but nobody seem to care to fix the problem. ---------------------------------------------------------------------- edgarrod71 · 2023-03-26 22:58 Now I upgraded to fpc 3.2.0 and saw that it's fixed using two variables...  Took a look on the latest version on gitlab.com and they put new things like ExpandFileName... Seem to be unnecesary... Are they starting to make spaghetti code?  Would somebody put me in contact with weblaz developers?  I'd like to help with cleaner code. ---------------------------------------------------------------------- PascalDragon · 2023-03-27 21:15 Quote from: edgarrod71 on March 27, 2023, 12:58:57 amNow I upgraded to fpc 3.2.0 and saw that it's fixed using two variables...  Took a look on the latest version on gitlab.com and they put new things like ExpandFileName... Seem to be unnecesary... Then you clearly don't understand what that code is trying to fix. Quote from: edgarrod71 on March 27, 2023, 12:58:57 amAre they starting to make spaghetti code? No, correctly working code. Quote from: edgarrod71 on March 27, 2023, 12:58:57 amWould somebody put me in contact with weblaz developers?  I'd like to help with cleaner code. If you think you have improvements for some code then do a merge request. MvC will then either pick it up or tell you why he doesn't agree. ---------------------------------------------------------------------- edgarrod71 · 2023-03-28 01:32 QuoteThen you clearly don't understand what that code is trying to fix. Oh, really?  Sure! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Const  IDX : Array[THTTPVariableType] of Integer = // of byte ? word ?  Globally and name=IDX_VarType      (-1,             // hvUnknown       0,31,11,22,36,  // hvHTTPVersion, hvMethod, hvCookie, hvSetCookie, hvXRequestedWith       25,26,27,28,29, // hvPathInfo, hvPathTranslated, hvRemoteAddress, hvRemoteHost,hvScriptName       30,32,33,35);   // hvServerPort, hvURL, hvQuery, hvContent Const  IDX : Array[THeader] of Integer = // of byte!, besides it would be better to put IDX globally       (-1,                          // with name=IDX_Header       1,2,3,4,         //  4              -1,-1,-1,5,-1,   //  9        6,7,8,           // 12        9,-1,-1,-1,      // 16         10,12,-1,13,-1,  // 21         14,34,-1,15,-1,  // 26         -1,-1,16,17,-1,  // 31         18,-1,-1,-1,19,  // 36         20,21,-1,-1,     // 40         -1,-1,23,-1,     // 44         -1,-1,24);       // 47 procedure THttpHeader.SetHttpVariable(aVariable: THttpVariableType; aValue: String);var idx: word;begin  idx := ord(aVariable);  if FVariables[idx] = aValue then    exit;  FVariables[idx] := aValue;  if (aVariable = hvCookie) and (aValue <> '') then    ParseCookies;end; procedure THTTPHeader.SetHeader(AHeader: THeader; const AValue: String);begin  FFields[ord(aHeader)]:=AValue;end; procedure THTTPHeader.SetServerPort(AValue: Word);begin    // SetFieldValue(30,IntToStr(AValue));  // 30? seriously?  SetHttpVariable(hvServerPort, IntToStr(aValue); // better!end; function THTTPHeader.GetServerPort: Word; begin   // Result:=StrToIntDef(GetFieldValue(30),0);  Result := ord(hvServerPort);  // or end;   procedure THTTPHeader.SetContentLength(Value: Integer);begin     // SetFieldValue(9,IntToStr(Value));    SetHeader(hhContentLength, IntToStr(aValue); // better!end; function THTTPHeader.GetContentLength: Integer; begin   // Result:=StrToIntDef(GetFieldValue(9),0); // instead of 9! Result 13  Result := ord(hhContentLength);end; QuoteNo, correctly working code. You only see the surface... deep inside there is redundant code, for instance checking files and directories... I was younger and after all these years, I understand that young programmers don't accept they can be wrong... it's simply ego...  I want to help because I love this language. BTW, thanks for your comments... ---------------------------------------------------------------------- PascalDragon · 2023-03-28 20:01 Quote from: edgarrod71 on March 28, 2023, 03:32:14 amQuoteThen you clearly don't understand what that code is trying to fix. Oh, really?  Sure! It doesn't help if you reply with a completely different example to what you had previously complained about. Quote from: edgarrod71 on March 28, 2023, 03:32:14 amQuoteNo, correctly working code. You only see the surface... deep inside there is redundant code, for instance checking files and directories... And you don't seem to see that more often than not there is a reason why the code is the way it is even if you don't necessarily see it right away (and yes, sometimes I have that problem as well when looking at unfamiliar code). For example as said, the ExpandFileName mentioned previously is there for a reason. ---------------------------------------------------------------------- edgarrod71 · 2023-03-29 01:26 QuoteIt doesn't help if you reply with a completely different example to what you had previously complained about.Simply I was busy and remember I come from fpc 3.0.4, I upgraded to 3.2.0, saw that code works but has redundancy, checked latest version on git, but let's face it, I know Pascal since I was 17 on first Macintosh, now I'm 51 so I'm from old school, but like Arnold said, Old but not Obsolete... lol.  I love to write clean code.  I simply put this new code to show you that I know what I'm talking about. Your second paragraph is hard to read... but you're right, when I upgrade again and start to code with latest fpc I'll have another point of view on the hill...