stratdev3 / SimpleW

SimpleW is a Simple Web Server library in NET (windows/linux/macos). It provides a cross-plateform framework for building web applications.
https://www.nuget.org/packages/SimpleW
MIT License
24 stars 2 forks source link

Is it possible to make this Net Framework compatible? #2

Open veso266 opened 2 weeks ago

veso266 commented 2 weeks ago

Hi there, was wondering if its possible to make your library compatible with Net framework (my app is curently targeting 4.7.1 (I could upgrade to Net framework 4.8.1)) and its not possible to migrate to net core

I came here from NetCoreServer

After migrating the parts I needed (HTTP and TCPServer) to NET Framework (attached below) NetCoreServerExample.zip

I discovered that NetCoreServer is actualy not that easy to use, so I found your wonderfull library I thought, ok, that should be easy, I just drop in my migrated NetCore server (comment out WebSocket parts for now (I intended to migrate WebSocket part later) and it will work

but sadly I was wrong and problems just started to creep out

  1. LitJWT was not compatible with NET Framework (I said, ok, lets get rid of JWT, and I will port it later)
  2. Then I started having problems with JSON serialization/deserialization, so I tried to get rid of JSON support (thinking, ok, not a problem, will do serialization/deserialization myself if I ever need it, but json was so tightly integrated into the library, that it was not possible to get rid of it)

So after a few days of work, I decided to call it quits and ask here for help :smile:

So I was wondering if you (since u made this library and know the arhitecture) would maybe manage to make this net framework compatible?

I am sure this would help many people because your library makes it realy easy to spin a simple REST server or serve static content, and because it uses NetCoreServer it could be even possible to add HTTP2 and even HTTP3 support to NetCoreServer while keeping rest api intact

veso266 commented 2 weeks ago

Almost did it, it can already serve static files HTTP Server EvenBetterNG.7z.zip

Now I just have to figure out, why this damm json doesn't want to work (I think I migrated something wrong in NetCoreServerExtension2.cs

stratdev3 commented 2 weeks ago

Hi,

I will look into it. If minor changes can result as i net framework support, i will do it.

veso266 commented 2 weeks ago

Thats great, if it helps I think I am getting closer to the problem that I am still having with REST api not working in NET framework example I showed you

in Route.cs at this function

private void ParseRoute()
        {

            // url
            var pos = RawUrl.IndexOf("?");
            var relativePath = pos > 0 ? RawUrl.Substring(0, pos) : RawUrl;

            // parse RouteAttribute looking for "{parameter}"
            IEnumerable<string> parameters = new Regex(@"{([^}]+)}").Matches(relativePath)
                                             .Where(m => m.Success)
                                             .Select(m => m.Groups[1].Value);

            foreach (var parameter in parameters)
            {
                // check for uniqueness
                if (!parameters_name.Contains(parameter))
                {
                    parameters_name.Add(parameter);
                }
            }

            // Construct regex startings with relativePath
            var pattern = relativePath;

            // the wildcard "*" must be convert to a valid regexp
            pattern = pattern.Replace("*", "(.*)");

            // regexp also contains RouteAttribute parameters
            if (parameters_name.Count > 0)
            {
                foreach (var parameter in Handler.Parameters.Keys) 
                {
                    pattern = pattern.Replace("{" + parameter.Name + "}", "([^/]+)");
                }
            }
            pattern = "^" + pattern + "$";
            regex = new Regex(pattern);
        }

I discovered that if I replace this

            IEnumerable<string> parameters = new Regex(@"{([^}]+)}").Matches(relativePath)
                                             .Where(m => m.Success)
                                             .Select(m => m.Groups[1].Value);

with this

var regex = new Regex(@"{([^}]+)}");
var matches = regex.Matches(relativePath).Cast<Match>();  // Convert to IEnumerable<Match>
IEnumerable<string> parameters = matches
                                   .Where(m => m.Success)
                                   .Select(m => m.Groups[1].Value);

then REST api stops working

veso266 commented 2 weeks ago

Hi there, here ya go, ported to NET Framework 4.8

HTTP Server.7z.zip

I tested almost all examples, they all work, exept this one: https://github.com/stratdev3/SimpleW?tab=readme-ov-file#path-parameters

but this one doesn't even work on NET Core, so not sure whats going on here, I always get 404 not found

Hope u like it :smile:

LazyDodo commented 2 weeks ago

I tested almost all examples, they all work, exept this one: https://github.com/stratdev3/SimpleW?tab=readme-ov-file#path-parameters

That's a bug in the example, RegExpEnabled needs to be set for that one to work