Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with finding some headers #491

Closed
mmdm95 opened this issue Nov 21, 2020 · 5 comments
Closed

Problem with finding some headers #491

mmdm95 opened this issue Nov 21, 2020 · 5 comments
Labels
awaiting-release This issue is currently waiting to be released feature

Comments

@mmdm95
Copy link

mmdm95 commented Nov 21, 2020

Inside Request class and inside construct method you have following code that behave wrong (at least for me!)

$this->headers[strtolower($key)] = $value;
$this->headers[strtolower(str_replace('_', '-', $key))] = $value;

I replace this with below code to have some headers like X-CSRF-TOKEN that was not recognizable before

if (substr($key, 0, 5) == 'HTTP_') {
    $this->headers[str_replace(' ', '-', strtolower(str_replace('_', ' ', substr($key, 5))))] = $value;
} else {
    $this->headers[strtolower(str_replace('_', '-', $key))] = $value;
}
@skipperbent
Copy link
Owner

What webserver are you using? I would be greatfull if you could post your debug-info (see documentation), it will help me debug the error and contains vital information about your webserver and it's header format.

@mmdm95
Copy link
Author

mmdm95 commented Mar 18, 2021

Hi, thanks for reply.

As I mentioned before, I test X-CSRF-TOKEN header and request class was not able to detect it.

  1. I am using apache webserver
  2. I did send a request through ajax with post method
  3. and add csrf token in X-CSRF-TOKEN header
  4. It'll throw TokenMismatchException inside BaseCsrfVerifier class because it could not detect csrf token
  5. the token was not under x-csrf-token key inside headers and it was inside http-x-csrf-token key

The code to detect headers was not optimized but I fix it with mentioned code above and it's ok now.

Also because it'll regenerate csrf token each time after detection, if you send some ajax requests, it'll have problem.

As I can see you are going to create new version. If you can fix some issues that I had and some features, it would be great:

  1. Add the ability for class hinting for controller and method
SimpleRouter::get('/some-route', [SomeController::class, 'someMethod']);
  1. An issue I had, was through submitting a form like:
<form method="post" action="<?= url('somewhere')->getRelativeUrl(); ?>">
   <input type="text" name="test">
   <button type="submit">submit</button>
</form>

It does not work because getRelativeUrl method add an extra slash after route and form will not submit!
If there was some method like getRelativeUrlTrimmed that trim forward slash from end of route, it'll be nice.

  1. A fixation for getting value from a method:
// if I use this
$val = input()->post('some_value', null);

// then I must check it through null like
if(null !== $val) {
   echo $val->getValue();
}

// but if I don't want the value and just want the check
if('x' == input()->post('some_value', null)->getValue()) {
   echo $val->getValue();
}

// as you can see `getValue()` method can not be apply to `null`
// if all values be an `InputItem` object, it would be much easier to use

Thanks for your attention

@skipperbent
Copy link
Owner

Hi again.

Thanks for the feedback and the detailed explanation, I really appreciate it!

From what you are describing it sounds like the browser and/or ajax is adding some additional naming to the header. I will do some testing and try to add support for better header parsing; it seems like webservers and browsers all have their own ways of specifying the headers.

Great ideas! I will fix the url bugs right away and try to add class hinting feature to the upcoming release.
I really like the last suggestion too as it does minimize the clutter, however I'm afraid it will/could break peoples existing setup - so it might have to be added as a feature for a major release. I've added some labels and will look into that feature in the future.

/ Simon

@skipperbent skipperbent removed the bug label Mar 18, 2021
@skipperbent
Copy link
Owner

I cannot recreate the url('somewhere')->getRelativeUrl(). Seems like it correctly trimmed on both the getAbsoluteUrl and getRelativeUrl methods.

I've tried routes like /contact and /contact/.

@skipperbent
Copy link
Owner

This issue has been resolved in version 4.3.0.0.
Feel free to open another issue if you are still experiencing issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-release This issue is currently waiting to be released feature
Projects
None yet
Development

No branches or pull requests

2 participants