Can you POST a form to a struts Action, from an external site? - struts

I am working with a third-party site which POSTs a response to a URL that I specify. When debugging the Action class which handles this path, I see that the request parameters are blank after the redirect.
My provided url is of the form http://example.com/Registration.do
This is my struts-config.xml entry.
<action path="/Registration" type="com.example.RegistrationAction">
<forward name="success" path="/registration/success.jsp"></forward>
</action>
Is there a way to submit a form from an external site, so that the request parameters will be passed correctly?

Related

How does Web Api 2 handle encoding?

I originally asked the following question, to get some anwsers on how to handle special characters in an URL (GET) request for my Web Api:
Web Api 2 routing issue with special characters in URL
Encoding was obviously the way to go. But in order to get everything working, i had to do a pretty nasty workaround. And now, Im' at the point where i don't really understand why my workaround had to be done in the first place. So, the following is my setup:
A client can call my Web Api 2, Hosted on iis 8.5, by a get request containing an email in the URL. The most extreme example would be the following email:
!$%&'*+-/=?^_`{}|~#test.com
And yes, that sucker is a valid email, which therefore the API has to support. The URL pattern is as follows:
.../api/permissions/{email}/{brand}/
So a get request would be something along the lines of this:
.../api/permissions/#!$%&'*+-/=?^_`{}|~#test.com/economy
As the marked answer to my other question suggests, encoding this url is obviously a necessity. But this left me with a couple of other issues, such as "double escape of characters not allowed", and some specific "404 - not found" (routing could not pass the url). This i could mange to handle with the following settings for iis:
<system.web>
...
<httpRuntime requestPathInvalidCharacters=""/>
</system.web>
<system.webServer>
<security>
<requestFiltering allowDoubleEscaping="true"/>
</security>
...
</system.webServer>
Now i was able to call my method with those pesky special characters, and everything was fine. But i hit another bump. The email specified above, #!$%&'*+-/=?^_`{}|~#test.com, resulted in a 404 - not found. An actual 404 - not found. The routing couldn't handle the request url:
[Route("{email}/{brand}"]
As I understand it, the iis decodes the request url, passes it on to the iis request pipeline, it is then picked up by the web api and run through the http message handlers before hitting the controller. In the controller, i could clearly see that the email part of the url was no longer encoded (provided i used a simple encoded email. the encoded email "#!$%&'*+-/=?^_`{}|~#test.com" still responded 404). I quickly figured, that the routing probably couldn't handle the fragmentation inside the url path, as the iis passes on a decoded url to the web api. So i had to get the url reencoded in the iis before handed on to the web api.
This i was able to make a workaround for, by using Url ReWrite. It reencoded that specific part of the url containing the email, and now the routing was handled properly with the special-character-email. The expected method was hit, and i could just decode the encoded email. To briefly sum up, this was the flow:
Request flow
Now, we have set up a LogMessageHandler which logs incoming requests and outgoing responses. When the logger logs the request.RequestUri, it is clear that the email is double encoded. But when the controller method is hit, it is only encoded once! So.. My question is, why do i have to reencode the URL in the iis for the routing to handle the request properly, when the url is already automatically encoded (and decoded again before hitting the controller)? Is this something i can configure? Can i somehow extend the scope of which the URL is encoded, all the way to the controller??
Regards
Frederik

MVC application most page requests being redirected to /login.aspx?ReturnUrl=

I have installed an MVC4 application provided by a third party on our web server and most requests are being redirected to http://domain.com/login.aspx?ReturnUrl=requestedpage.
The website does not have any form of authentication and it does not have this or any other login page.
The default page loads but none of the page resources (like images, CSS, etc) loads as each of those is being redirected to the non-existent login.aspx page. Even a webservice request gets redirected.
I have done lots of searching online and tried the usual fix of <add key="autoFormsAuthentication" value="false" /> but it doesn't make any difference. I also tried <authentication mode="None" />.
It is IIS8.5 on Server 2012 and anonymous access is enabled throughout.
I just tried installing MVC4 directly on the server and this did not help.
Can anyone help?
This problem turned out to be caused by the anonymous user account not being set to the ApplicationPoolIdentity.
I fixed this after finding the answer on another question.

Custom 404 page for invalid request

I have implemented custom error pages in an MVC4 application. Basically I've added the following to my Web.Config file:
<customErrors mode="On" defaultRedirect="Error">
<error statusCode="404" redirect="~/Error/NotFound"/>
<error statusCode="500" redirect="~/Error/Index"/>
</customErrors>
Unfortunately, if someone uses a request that adds a path to a route the redirect doesn't take place. For example I have a Document method that takes an ID on the controller Content. The following request is valid:
/Content/Document/1
I get the 404 displayed when someone calls an url like this:
/BlaBla
/Content/BlaBla
Some users manage to add /BlaBla after the ID:
/Content/Document/1/Blabla
This is the case where my custom 404 page is not shown. How can I handle this?
A CatchAll - Route solved the problem. A question with an answer which made me find the solution:
MVC 4 catch all route never reached

url with extension not getting handled by routing

I've been folowing the advice from this article for setting up a robots.txt file in asp.net mvc3 for using a controller to handle the server response, and IIS 8.0 express is returning a file not found error, rather than an asp.net error.
How do I get IIS to not look for a file in these cases? Is there something I need in the web.config?
IIS tries to be intelligent here. He intercepts the dot in the url and thinks that this is a static file and attempts to serve it with the default StaticFile handler. it dopesn't event get to the managed ASP.NET application.
The first possibility is to add the following in your web.config
<system.webserver>
<modules runAllManagedModulesForAllRequests="true" />
but actually that's not something I would recommend you doing because this might have a negative effect on the performance of your application because now all requests to static files (such as .js, .css, images, ...) will go through the managed pipeline.
The recommended approach is to add the following handler to your web.config (<handlers> tag of <system.webServer>):
<system.webServer>
<handlers>
<add name="Robots-ISAPI-Integrated-4.0" path="/robots.txt" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
...
</handlers>
</system.webServer>
Notice how we have specified that this handler will only apply to a particular URL and HTTP verb.
Now when you GET /robots.txt, IIS will no longer handle it with the StaticFile handler but will instead pass it to the managed pipeline ASP.NET. And then it will be intercepted by the routing engine and routed to the corresponding controller action.
Unless you need a dynamically generated robots.txt file, which is very rarely necessary, just do the following:
Ignore the route to robots.txt
routes.IgnoreRoute("robots.txt");
Add the robots.txt file to your root dir

How to handle HttpWebRequest redirect with non-ascii characters

I am writing an application that gets the server response code for a set of URLS by using the HttpWebRequest class. I came across a URL today that is causing me problems.
The problematic URL is http://blip.tv/file/5312019
When I load this URL in Internet Explorer, it correctly redirects me to http://blip.tv/sorawut/money-talk-เม่า-นักเขียนการ์ตูนหุ้น-5329374. But when using the HttpWebRequest class, it's having a problem redirecting.
If I set AllowAutoRedirect to false and examine the Location metatag in the response.Headers collection, it is showing the funky URL http://blip.tv/sorawut/money-talk-à¹à¸¡à¹à¸²-à¸à¸±à¸à¹à¸à¸µà¸¢à¸à¸à¸²à¸£à¹à¸à¸¹à¸à¸«à¸¸à¹à¸-5329374. When the request attempts to redirect to this URL, it causes an infinite redirect loop and ultimately ends up throwing a WebException saying "Too many automatic redirections were attempted".
I tried pasting this funky URL into Internet Explorer and it automatically changed it to the correct redirect URL and successfully loaded the page.
So, what do I need to do to have my HttpWebRequest return a status code of 200 for this particular URL? (Since it is a valid and active URL after a successful redirect)
try this in your config file
<configuration>
<uri>
<idn enabled="All" />
<iriParsing enabled="true" />
</uri>
</configuration>
Look at the section International Resource Identifier Support here -> http://msdn.microsoft.com/en-us/library/system.uri.aspx