Recent

Author Topic: WebLaz corrections  (Read 1231 times)

edgarrod71

  • Jr. Member
  • **
  • Posts: 68
WebLaz corrections
« on: March 19, 2023, 08:59:48 pm »
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][+][-]
  1.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  2.   cthreads,
  3.   {$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][+][-]
  1. Procedure RegisterFileLocation(Const ALocation, ADirectory: String);
  2. begin
  3.   if ALocation = '' then
  4.     Raise HTTPError.Create(SErrNoLocation);
  5.   if Pos('/',ALocation)<>0 then
  6.     Raise HTTPError.Create(SErrInvalidLocation);
  7.   if Locations = Nil then
  8.     Locations:=TStringList.Create;
  9.   if ADirectory = '' then
  10.     Locations.Values[IncludeHTTPPathDelimiter(ALocation)] := ExtractFilePath(ParamStr(0))
  11.   else
  12.     if DirectoryExists(ADirectory) then
  13.       Locations.Values[IncludeHTTPPathDelimiter(ALocation)] := IncludeTrailingPathDelimiter(ADirectory)
  14.     else
  15.       Raise HTTPError.Create(SErrInvalidDirectory);
  16.   if DefaultFileModuleClass = Nil then
  17.     DefaultFileModuleClass := TFPCustomFileModule;
  18.   RegisterHTTPModule(ALocation, DefaultFileModuleClass, true);
  19. 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.   ;D

egsuh

  • Hero Member
  • *****
  • Posts: 1121
Re: WebLaz corrections
« Reply #1 on: March 20, 2023, 02:34:39 am »
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

  • Jr. Member
  • **
  • Posts: 68
Re: WebLaz corrections
« Reply #2 on: March 21, 2023, 02:54:09 pm »
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

  • Jr. Member
  • **
  • Posts: 68
Re: WebLaz corrections
« Reply #3 on: March 22, 2023, 10:54:41 pm »
Lol... I fixed it!  Must work on every platform

egsuh

  • Hero Member
  • *****
  • Posts: 1121
Re: WebLaz corrections
« Reply #4 on: March 23, 2023, 07:22:09 am »
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

  • Jr. Member
  • **
  • Posts: 68
Re: WebLaz corrections
« Reply #5 on: March 24, 2023, 05:17:29 am »
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

  • Jr. Member
  • **
  • Posts: 68
Re: WebLaz corrections
« Reply #6 on: March 27, 2023, 12:58:57 am »
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. ;)
« Last Edit: March 27, 2023, 03:05:29 am by edgarrod71 »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5076
  • Compiler Developer
Re: WebLaz corrections
« Reply #7 on: March 27, 2023, 11:15:23 pm »
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...

Then you clearly don't understand what that code is trying to fix.

Are they starting to make spaghetti code?

No, correctly working code.

Would 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

  • Jr. Member
  • **
  • Posts: 68
Re: WebLaz corrections
« Reply #8 on: March 28, 2023, 03:32:14 am »
Quote
Then you clearly don't understand what that code is trying to fix.

Oh, really?  Sure!

Code: Pascal  [Select][+][-]
  1. Const
  2.   IDX : Array[THTTPVariableType] of Integer = // of byte ? word ?  Globally and name=IDX_VarType
  3.       (-1,             // hvUnknown
  4.        0,31,11,22,36,  // hvHTTPVersion, hvMethod, hvCookie, hvSetCookie, hvXRequestedWith
  5.        25,26,27,28,29, // hvPathInfo, hvPathTranslated, hvRemoteAddress, hvRemoteHost,hvScriptName
  6.        30,32,33,35);   // hvServerPort, hvURL, hvQuery, hvContent
  7.  
  8. Const
  9.   IDX : Array[THeader] of Integer = // of byte!, besides it would be better to put IDX globally
  10.       (-1,                          // with name=IDX_Header
  11.        1,2,3,4,         //  4      
  12.        -1,-1,-1,5,-1,   //  9
  13.        6,7,8,           // 12
  14.        9,-1,-1,-1,      // 16  
  15.        10,12,-1,13,-1,  // 21  
  16.        14,34,-1,15,-1,  // 26  
  17.        -1,-1,16,17,-1,  // 31  
  18.        18,-1,-1,-1,19,  // 36  
  19.        20,21,-1,-1,     // 40  
  20.        -1,-1,23,-1,     // 44  
  21.        -1,-1,24);       // 47
  22.  
  23. procedure THttpHeader.SetHttpVariable(aVariable: THttpVariableType; aValue: String);
  24. var idx: word;
  25. begin
  26.   idx := ord(aVariable);
  27.   if FVariables[idx] = aValue then
  28.     exit;
  29.   FVariables[idx] := aValue;
  30.   if (aVariable = hvCookie) and (aValue <> '') then
  31.     ParseCookies;
  32. end;
  33.  
  34. procedure THTTPHeader.SetHeader(AHeader: THeader; const AValue: String);
  35. begin
  36.   FFields[ord(aHeader)]:=AValue;
  37. end;
  38.  
  39. procedure THTTPHeader.SetServerPort(AValue: Word);
  40. begin  
  41.   // SetFieldValue(30,IntToStr(AValue));  // 30? seriously?
  42.   SetHttpVariable(hvServerPort, IntToStr(aValue); // better!
  43. end;
  44.  
  45. function THTTPHeader.GetServerPort: Word;
  46. begin
  47.   // Result:=StrToIntDef(GetFieldValue(30),0);
  48.   Result := ord(hvServerPort);  // or
  49. end;  
  50.  
  51. procedure THTTPHeader.SetContentLength(Value: Integer);
  52. begin  
  53.   // SetFieldValue(9,IntToStr(Value));  
  54.   SetHeader(hhContentLength, IntToStr(aValue); // better!
  55. end;
  56.  
  57. function THTTPHeader.GetContentLength: Integer;
  58. begin
  59.   // Result:=StrToIntDef(GetFieldValue(9),0); // instead of 9! Result 13
  60.   Result := ord(hhContentLength);
  61. end;

Quote
No, 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

  • Hero Member
  • *****
  • Posts: 5076
  • Compiler Developer
Re: WebLaz corrections
« Reply #9 on: March 28, 2023, 10:01:05 pm »
Quote
Then 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
No, 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

  • Jr. Member
  • **
  • Posts: 68
Re: WebLaz corrections
« Reply #10 on: March 29, 2023, 03:26:46 am »
Quote
It 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...

 

TinyPortal © 2005-2018