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

Phalcon Double Sends Headers & Cookies #12908

Closed
Bellardia opened this issue Jun 19, 2017 · 11 comments
Closed

Phalcon Double Sends Headers & Cookies #12908

Bellardia opened this issue Jun 19, 2017 · 11 comments

Comments

@Bellardia
Copy link
Contributor

Bellardia commented Jun 19, 2017

Expected Behavior

Cookies and Headers get output once per request lifecycle.

Actual Behavior

Cookies and Headers set in \Phalcon\Http\Response will get output twice if a controller returns an object implementing ResponseInterface.

cphalcon/phalcon/mvc/application.zep inside handle

if typeof possibleResponse == "object" {
	if possibleResponse instanceof ResponseInterface {
		possibleResponse->sendHeaders();
		possibleResponse->sendCookies();
		return possibleResponse;
	}
}

cphalcon/phalcon/http/response.zep

public function send() -> <Response>
	{
		var content, file;

		if this->_sent {
			throw new Exception("Response was already sent");
		}

		this->sendHeaders();

		this->sendCookies();

		/**
		 * Output the response body
		 */
		let content = this->_content;
		if content != null {
			echo content;
		} else {
			let file = this->_file;

			if typeof file == "string" && strlen(file) {
				readfile(file);
			}
		}

		let this->_sent = true;
		return this;
	}
Set-Cookie:test=test; Max-Age=0; path=/; domain=.localhost
Set-Cookie:test=test; Max-Age=0; path=/; domain=.localhost
@Jurigag
Copy link
Contributor

Jurigag commented Jun 19, 2017

Just don't use send method if you return response? Application doesn't call send anywhere so not sure what is your problem. I don't have double output - because i don't call send method.

@Bellardia
Copy link
Contributor Author

Bellardia commented Jun 19, 2017

@Jurigag

Are you using Micro or MVC?

The send() method is not called automatically in all configurations, which may mean while it works for you it is still not correct.

cphalcon/phalcon/mvc/micro.zep

/**
 * Check if the returned object is already a response
 */
if typeof returnedValue == "object" {
	if returnedValue instanceof ResponseInterface {
		/**
		 * Automatically send the response
		 */
		 if !returnedValue->isSent() {
		 	returnedValue->send();
		 }
	}
}

Here we can see micro correctly calls the send() method on the response, but this is not done in MVC.

cphalcon/phalcon/mvc/application.zep

if typeof possibleResponse == "object" {
	if possibleResponse instanceof ResponseInterface {
		possibleResponse->sendHeaders();
		possibleResponse->sendCookies();
		return possibleResponse;
	}
}

As Phalcon\Http\Response::send() outputs headers, you cannot avoid the double send because Phalcon\Mvc\Application::handle() will also send headers.

@Jurigag
Copy link
Contributor

Jurigag commented Jun 19, 2017

I am using MVC, but what micro has to do with application? It's different kernel, you shouldn't use both at the same time. So i still don't know what's your issue.

Just to echo content in MVC application:

    echo $application->handle()->getContent();

No need for send method.

@Bellardia
Copy link
Contributor Author

I'm not using Micro, it was an inquiry.

I'm not sure where we differ here but if you follow the code path there's an issue. I appreciate your taking the time to respond, but you're not providing any productive feedback.

@Jurigag
Copy link
Contributor

Jurigag commented Jun 19, 2017

I'm not using too and i don't have this issue.

@Bellardia
Copy link
Contributor Author

With all do respect you're not being helpful.

getContent() is not the same function as send(). The question is as to why handle() decides to send headers but not output makes little sense from an API perspective.

getContent() does not update class flags such as _sent. Which again, leads me to believe this is a bug.

@grigoriy-ivanov
Copy link

I found PR #12378 ac254c9

@Bellardia
Copy link
Contributor Author

Bellardia commented Jun 19, 2017

@Grigoriygithub Amazing, that's exactly the issue :) Thanks!!

@Jurigag
Copy link
Contributor

Jurigag commented Jun 20, 2017

Yea, PR is fine i guess, just not sure if we can add it in phalcon 3. I guess you will need to wait for phalcon 4.

@TheCrimsonIdol
Copy link

??? this should not be.

@sergeyklay
Copy link
Contributor

Fixed in the 4.0.x branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants