Sushan Ghimire
Sushan Ghimire

Reputation: 7567

How to differentiate request coming from command-line and browsers?

To check whether it is a cli or http request, in PHP this method php_sapi_namecan be used, take a look here. I am trying to replicate that in apache conf file. The underlying idea is, if the request is coming from cli a 'minimal info' is served, if the request is from browsers then the users are redirected to different location. Is this possible?

MY PSEUDO CODE:

IF (REQUEST_COMING_FROM_CLI) {
  ProxyPass          /      http://${IP_ADDR}:5000/
  ProxyPassReverse   /      http://${IP_ADDR}:5000/
}ELSE IF(REQUEST_COMING_FROM_WEB_BROWSERS){
  ProxyPass          /      http://${IP_ADDR}:8585/welcome/
  ProxyPassReverse   /      http://${IP_ADDR}:8585/welcome/
}

Addition: cURL uses host of different protocols including http, ftp & telnet. Can apache figure out if the request is from cli or browser?

Upvotes: 1

Views: 1750

Answers (3)

bernal varela
bernal varela

Reputation: 31

The only way to do this is to test the user agent sent in the header of the request but this information can be easily changed.

By default every php http request looks like this to the apache server:

192.168.1.15 - - [01/Oct/2008:21:52:43 +1300] "GET / HTTP/1.0" 200 5194 "-" "-"

this information can be easily changed to look like a browser, for example using this

ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3');

the http request will look like this

192.168.1.15 - - [01/Oct/2008:21:54:29 +1300] "GET / HTTP/1.0" 200 5193 "-" "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3"

At this moment the apache will think that the received connection come from a windows firefox 3.0.3.

So there is no a exact way to get this information.

Upvotes: 2

SKYWALKR
SKYWALKR

Reputation: 620

You can use a BrowserMatch directive if the cli requests are not spoofing a real browser in the User-Agent header. Else, like everyone else has said, there is no way to tell the difference.

Upvotes: 1

brilletjuh
brilletjuh

Reputation: 117

For as far as I know, there is no way to find the difference using apache.

if a request from the command-line is set up properly, apache can not make a difference between command-line and browser.

When you check it in PHP (using php_sapi_name, as you suggested), it only checks where php itself was called from (cli, apache, etc.), not where the http request came from.

using telnet for the command line, you can connect to apache, set the required http-headers and send the request as if you were using a browser(only, the browser sets the headers for you)

so, i do not think apache could differentiate between console or browser

Upvotes: 2

Related Questions