How do I detect mobile clients on my web server? - mobile

When http request comes to my server, how do I detect if it is from iphone, android, or other devices?

You can grab the User Agent. That tells what browser type it is (iphone, chrome, ie, anything)
To help you:
http://whatsmyuseragent.com/
http://en.wikipedia.org/wiki/User_agent

You need to check the header of the HTTP request. You can find both the OS and the browser being used in the "User-Agent" field.
If you are using javascript then use the navigator object
navigator.userAgent
If you are using php then you can access the HTTP header
$userAgent = $_SERVER["HTTP_USER_AGENT"];

As #dave-delong states in his response you can use the User-Agent HTTP header.
But User-Agent can be quite hard to parse.
I recommend you to use third party libraries for parsing User-Agent and detecting mobile.
On Node.js
Apparently OP uses Node.js and then can use mobiledetect.js (demo).
Detect the device by comparing patterns against a given User-Agent string (phone, tablet, desktop, mobile grade, os, versions).
const MobileDetect = require('mobile-detect');
const md = new MobileDetect(req.headers['user-agent']);
console.log(md.mobile()); // 'Sony'
console.log(md.phone()); // 'Sony'
console.log(md.tablet()); // null
On PHP
On a PHP server mobiledetect (demo).
Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.

Related

Does kiwi tcms has Node.js api?

I practice to use tcms_api in python now, but i want to use node.js to call kiwi api.
I did not find the relevant information on the network.
Can anyone give me some suggestions??
Thanks.
The API is exported both over XML-RPC and JSON-RPC protocols. The JSON-RPC protocol is very simple, see https://www.jsonrpc.org/specification and you can easily create your own client.
While Kiwi TCMS doesn't provide a stand-alone JavaScript client you can use the following as an example:
https://github.com/kiwitcms/Kiwi/blob/master/tcms/static/js/jsonrpc.js
Warning: the code above doesn't handle authentication b/c it is designed to execute in the browser which already keeps a session cookie after the user has been logged in. If you are designing a generic client that will be used outside the browser your first call should be to the Auth.login method which returns the session ID and also sets a Cookie header in the response. The existing Python API client just parses the Cookie header and provides it on subsequent requests, see:
https://github.com/kiwitcms/tcms-api/blob/master/tcms_api/xmlrpc.py#L19

How can I get the Current URL Protocol in DNN?

How can I get Https or Http in the backend code of a DNN module?
Currently, I have got this code, but I need the protocol (https:// or http://)
PortalSettings.Current.PortalAlias.HTTPAlias + PortalSettings.HomeDirectory + PortalSettings.LogoFile;
You can maybe use this to check if the current tab has https enabled (to check the request itself you can use Request.IsSecureConnection)
PortalSettings.ActiveTab.IsSecure
And/or
PortalSettings.SSLEnabled;
PortalSettings.SSLEnforced;
PortalSettings.SSLURL;
If you want it for the current URL, you can use the regular .NET API's as well.
HttpContext.Current.Request.Url.Scheme
That will return http or https based on the current request URL. Its the fastest way that I'm aware of to do this.

Is a single Cookie Based API for multiple frontends possible from a CORS perspective?

I originally wrote an REST API to work with a previously written mobile app. The mobile programmer requested from me to generate an auth_token on login that he will pass as a header on each request that needed authentication. This API runs at api.example.com.
Later on, I was commissioned to write an AngularJS app that communicates with this API, so I had to use Access-Control-Allow headers on the backend for OPTIONS requests to be CORS compatible CORS so my browser allows the connection (looks like iOS does not look for this headers). This app runs at one.example.com.
Now, I have to write a second AngularJS app that will run at two.example.com and there's a third being planned for the near future at three.example.com.
My problem is that my Access-Control-Allow-Origin header looks like this:
Access-Control-Allow-Origin: http://one.example.com:80
* is not allowed, nor I'm able to set this header to more than one origin. So as far as I can see I have two solutions:
Implement token-based authentication in parallel to the current cookie-based one. I'm thinking on this. This will of course take some time I'm willing to save.
Send the requester a header or param to the API endpoint identifying the app on the OPTIONS request and server-side, produce the CORS headers accordingly. I don't even know if it's possible and this looks nasty for even thinking it.
Any better ideas?
If they have the same origin, example the same domain (example.com) or the same subdomain (1.ex.example.com and 2.ex.example.com) they can share the same cookie. Because cookie is based on the domain itself.

App Engine Accept-Encoding

In the APP Engine API, it is mentioned that, if the request comes with "Accept-Encoding" set, then it will automatically compress the response.
But when I look at the request, the header is not there. but at the browser, it is set. when I try to explicitly set the header(with JQuery ajax function), there is a message:
Refused to set unsafe header "Accept-Encoding"
But this situation is not occurring when working in local host - request has the "Accept-Encoding" header. this happens only after publishing. but not allowing to set the "Accept-Encoding" explicitly happens always.
I searched everywhere, but couldn't find a explanation to the problem. It would be really helpful if someone can explain...
You have two different problems:
App Engine does not compress reply. GAE uses a number of factors to determine if response needs to be compressed. It takes content type and user agent into account when deciding. See the answer by Nick Johnson (from GAE team).
jQuery refuses to set "Accept-Encoding" header. Note that this is a jQuery issue and has nothing to do with GAE. See this: Is it possible to force jQuery to make AJAX calls for URLs with gzip/deflate enabled?
I have a similar problem as in the HTTPRequest header, "Accept-Encoding" is null. As GAE has explained it looks for Accept-Encoding and User-Agent headers, if it wants to compress. but in my case there is no way the GAE can recognize whether to compress.
From the browser, then header is set, but in the request header, it is not.

iphone + cookies

Does iphone support cookies????
iPhone is simply a piece of hardware so, no it doesn't support cookies. It's the built-in Safari web browser which supports cookies.
Look at: NSHTTPCookie and NSHTTPCookieStorage
URL Loading System Overview says:
The NSHTTPCookie class encapsulates a
cookie, providing accessors for many
of the common cookie attributes. It
also provides methods to convert HTTP
cookie headers to NSHTTPCookie
instances and convert an NSHTTPCookie
instance to headers suitable for use
with an NSURLRequest. The URL loading
system automatically sends any stored
cookies appropriate for an
NSURLRequest. unless the request
specifies not to send cookies.
Likewise, cookies returned in an
NSURLResponse are accepted in
accordance with the current cookie
acceptance policy.
The NSHTTPCookieStorage class provides
the interface for managing the
collection of NSHTTPCookie objects
shared by all applications.
Get cookies using iPhone SDK
Any modern browser can't survive without supporting cookies

Resources