Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Let introduce the request state to keep it between events and simplify error logging. #690

Closed
wants to merge 45 commits into from

Conversation

volanavlad
Copy link

See basic sample

Doneness:

  • [*] Build is okay - I made sure that this change is building successfully.
  • [*] No Bugs - I made sure that this change is working properly as expected. It doesn't have any bugs that you are aware of.
  • [*] Branching - If this is not a hotfix, I am making this request against the master branch

honfika and others added 30 commits July 20, 2019 12:22
do not throw exception in fillbuffer (only when it called on an alrea…
netcoreapp2.1 target removed, use netstandard 2.1
add IsSocketReuseAvailable
enable socket based on framework version via RunTime.IsSocketReuseAvailable
enable socket reuse based on framework via RunTime.IsSocketReuseAvailable
fix compatibility bug
missed one
Enable socket reuse based on framework
@honfika
Copy link
Collaborator

honfika commented Nov 30, 2019

There is already a SessionEventArgs class, it contains the state of the request. I think it is unnecessary to add another object.

There are too many garbage in this PR, for example Titanium.Web.Proxy.Examples.Wpf_lmb5j4or_wpftmp.csproj file. It looks a temporary file.
Also a lot of merge commits and commits from other users. So it looks a little bit strange.

Why did you rename the ProxyServer class to ProxyServer base?

What do you want to achive exaclty?

@ByronAP
Copy link
Contributor

ByronAP commented Dec 1, 2019

💥 🏃 👀

@volanavlad
Copy link
Author

SessionEventArgs is not extensible by custom state.

Moreover SessionEventArgs is too late. I need to share state between all pipeline events, i.e. including

ServerCertificateValidationCallback;
ClientCertificateSelectionCallback;
OnClientConnectionCreate;
OnServerConnectionCreate;
onBeforeTunnelConnectRequest
onBeforeTunnelConnectResponse
multipartRequestPartSent
etc..

and of course I need to have access to my (custom, extended) state in error handler on any pipeline stage including socket internal errors.

Yes, I have renamed old ProxyServer class to ProxyServer base to minimize changes in code.
But I provide new ProxyServer with exactly same public api.
You can see that all tests and 2 other examples works well with no changes in its code.

You can see at basic sample that all that you need to implement custom state is to inherit from base request state and all other things became work fine.

public class ExtendedProxyState : RequestState<ExtendedProxyState>
{
	// this is my custom state and I can use it anywhere including my custom error handlers below
	internal StringBuilder PipelineInfo=new StringBuilder();
 
	public override async Task OnErrorAsync(Exception e)
	{
		await Console.Out.WriteLineAsync($"OnErrorAsync: {PipelineInfo} : {e}");
	}
	public override void OnError(Exception e)
	{
		Console.Out.WriteLine($"OnErrorAsync: {PipelineInfo}: {e}");
	}
}
// new proxy server with extended state but the same public API - no other coding need 
public class ProxyServer : ExtendedProxyState.ProxyServer
{
}

Please compile and debug Titanium.Web.Proxy.Examples.Basic sample at master branch in my fork.

I would make much more refactoring to simplify API but now I prefer to have code compatibility and simple merges with base repo.

Володько А В and others added 3 commits December 1, 2019 09:39
@honfika
Copy link
Collaborator

honfika commented Dec 1, 2019

SessionEventArgs is not extensible by custom state.

There is a UserData property in the SessionEventArgs(Base) class. You can set any custom state in that.

SessionEventArgs was already there in:
BeforeTunnelConnectRequest
BeforeTunnelConnectResponse

It was just added to ServerCertificateValidationCallback and ClientCertificateSelectionCallback callbacks because of issue #460, and also added to MultipartRequestPartSent

I can add the user data to OnClientConnectionCreate event if needed.

OnServerConnectionCreate is not really related to a single session, since the connections are re-used for new client connections, however it could receive the first user data.

I'm sorry, but currently I can't merge this PR. Please try to create a new one only with your changes.
Now this PR is overwriting a lot of other changes, please check the compare:
https://github.com/justcoding121/Titanium-Web-Proxy/pull/690/files?w=1

And also please create a clean PR:

  • as I already mentioned, there is a temproary csproj: Titanium.Web.Proxy.Examples.Wpf_lmb5j4or_wpftmp.csproj
  • code formatting is not following the source of TWP
  • add new line after }
  • no new line before {
  • space after comma in metgod arguents (and calls)
  • no space before comma in metgod arguents (and calls)
  • the ident was modified, for example check the ProxyTestController file:
    8a5cc3f#diff-99a6447bc3706ce43dce200ee1f400e3
  • there are merge problems for example in ProxyTestController writeToConsole method
  • websocket example which was added yesterday was removed (probabaly merge problem)
  • some code was just commented out, why? For example: //await @lock.WaitAsync();

@volanavlad
Copy link
Author

No problem.
I will keep my own fork.

honfika added a commit that referenced this pull request Dec 1, 2019
@volanavlad
Copy link
Author

User state in ProxyEventArgsBase? May be it solve my critical needs rigth now. But what about socket errors? Before client connection is established? How to diagnose it?

I will try to show how to do it in my fork. You are welcome to merge my code any time.

@ByronAP
Copy link
Contributor

ByronAP commented Dec 1, 2019

How would you have a socket error before a client makes a request? A listener error?

@honfika
Copy link
Collaborator

honfika commented Dec 2, 2019

A listen error can be catched when you call the ProxyServer.Start() method (Or the ProxyServer.AddEndPoint(), while the server is already running)

But I think there is no related changes for this in your fork.

Now the only differnece that I see is that you have an OnError method, which is only called when there is an exception in the User code of the ServerCertificateValidationCallback.

It is a good idea to add a non-global error handler to the request. I'll add it to the ProxyBaseEventArgs, so you will be able to subscribe for the errors of a single request. I plan to add an IsHandled property to the event arguments and when it is not true, the global error handler will be called (as it is now). And of course it should be fired for user calback errors and non user errors, too.

I appreciate your work, but next time if you create a PR, please be careful and do not overwrite other changes and do not reformat the code (using tabs), only when the purpuse of the PR is the code cleanup.

@volanavlad
Copy link
Author

OK

@volanavlad
Copy link
Author

volanavlad commented Dec 2, 2019

Next time I will try to foollow code formatting of the source of TWP.

But you need to merge your beta and stable branches to master.
Otherwise you will always get garbage on any PR from other repos.

@volanavlad volanavlad closed this Dec 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants