Although most Grails controllers render HTML, JSON or XML output it is possible to use them to render binary data as well. We use a controller to render images uploaded by editors into our content management interface. The theory is simple enough, instead of using the render
dynamic method or returning a model the controller action just writes bytes directly to the HTTP response stream. Our action looked something like this:
def show = {
def image = Image.read(params.id)
if (image) {
response.contentType = image.contentType
response.outputStream.withStream { stream ->
stream << image.bytes
}
} else {
response.sendError SC_NOT_FOUND
}
}
This seemed to work well enough. However when writing a test I noticed an odd thing. I was using RESTClient to scrape resource URLs out of and make a HEAD request against them to ensure the URLs were valid. Javascript and CSS files were working fine but all the non-static images in the page were getting 404s. Initially I suspected a data setup problem and spent some time ensuring my test was setting data up properly. It was only once I put some debug logging in the controller action that I saw that the controller was actually loading images. The 404 was not coming from the else block in the action as I initially assumed. I tried changing the RESTClient call from head to get and suddenly the image URLs started working!
Once I did that I realised what the problem was. An HTTP HEAD request does not expect a response, in fact a server receiving a HEAD request must not return a response. The response stream that our controller action is writing to is, when the request method is HEAD, actually a no-op stream. When the action completes Grails checks to see if anything has been committed to the response stream and since it has not assumes that we want to render a view by convention. You can probably see where this is going now. The convention is that the request gets forwarded to grails-app/views/<controller>/<action>.gsp
which of course does not exist. The forwarded request sets a response code of 404 because there is no GSP!
We caught this bug in our app completely by accident but it could actually have been quite serious. Caching proxies and CDNs may well use a HEAD request to revalidate content and on getting a 404 assume that the URL is no longer valid. If the 404 response itself then gets cached we could get broken images on our site because the CDN tells the client browser there's nothing there.
The solution is simple enough. I changed the controller action to simply set a 200 response code when it gets a HEAD request for a valid image:
def show = {
def image = Image.read(params.id)
if (image) {
if (request.method == "HEAD") {
render SC_OK
} else {
response.contentType = image.contentType
response.outputStream.withStream { stream ->
stream << image.bytes
}
}
} else {
response.sendError SC_NOT_FOUND
}
}
A neater solution might be to use Grails’ support for mapping actions to request methods so that GET and HEAD requests dispatch to different actions.
27 comments:
Although strictly speaking the head request should return the number of bytes in the Content-Length header that would be returned in the get request's stream?
Maybe we need a withRequestMethod block. Could be nice.
Her voice,her bright body.Her infinite hike81ai eyes.I no longer love her,
that's certain,but maybe I love her.
Cheap New Era Hats
New Era Fitted Hats
Wholesale Cheap Hats
Cheap Hats
Cheap NCAA Hats
Great Code :) Thanks for Sharing
Wedding Websites
Thanks for sharing this quality information with us and i like this.
juegos kizi | happy wheels | juegos kizi | agario | agar io | my little pony games | fireboy and watergirl
You need to kill time, you need entertainment. Refer to our website. hope you get the most comfort.
Thanks you for sharing!
Friv 4
Play the Best Free Games! We've picked out the racing games, cooking games, candy crush, games shooting, fashion games, ...
Thanks for sharing !
Kizi 200
Friv 1
Y88
Y9
Thanks for sharing. I hope it will be helpful for too many people that are searching for this topic.
run 2 game | slitherio | run 2 unblocked | slither io | run 2 game | slither.io
The article clearly and plainly as I will learn and focus on it baixar sonic dash , baixar sonic dash , baixar sonic dash , baixar sonic dash , baixar sonic dash , baixar sonic dash , baixar sonic dash , baixar sonic dash , baixar sonic dash , baixar sonic dash , baixar sonic dash , baixar sonic dash , baixar sonic dash , baixar sonic dash , baixar sonic dash
baixar sonic dash , baixar sonic dash , baixar sonic dash , baixar sonic dash , baixar sonic dash , baixar sonic dash , baixar sonic dash, baixar sonic dash, baixar sonic dash , baixar sonic dash , baixar sonic dash , baixar sonic dash , baixar sonic dash
Very helpful advice in this particular post! It’s the little changes that make the largest changes. Thanks for sharing!
strike force kitty 2 | swords and souls 2 | red ball 3 | ninjago games
شركة عزل اسطح بالدمام
شركة عزل اسطح بالخبر
شركة مكافحة حشرات بالدمام
شركة مكافحة حشرات بالخبر
شركة كشف تسربات المياة بالدمام
شركة كشف تسربات المياة بالخبر
شركة تنظيف مجالس بالدمام
شركة تنظيف مجالس بالخبر
شركة تنظيف سجاد بالدمام
شركة تنظيف سجاد بالخبر
شركة تنظيف بالدمام
شركة تنظيف بالخبر
شركة تسليك مجاري بالدمام
شركة تسليك مجاري بالخبر
super post live cricket stream, webcric live, crictime, smartcric
Thanks for your page! Your share information it helped me alot! I pray for you to be happy and successful in your life.
bloxorz
it's so simple yet beautiful! I believe evryone will thank us for your post!
slither io
Thanks for the informations you shared!! I hope you will continue to have similar posts to share with everyone!
slither io
شركة كشف تسربات المياه بالخبر
شركة تنظيف بيارات بالدمام
شركة رش مبيدات بالاحساء
اتود الوصول لافضل عمليات التنظيف مع الحصول علي ضمان لاطول فترة خصومات وعروض رائعه لعمليات التنظيف يمكنك الاستمتاع بالخدمة عن طريق زيارة الرابط التالي
شركة تنظيف فلل بالاحساء
شركة تنظيف شقق بالدمام
iWantv! 'ABS-CBN live spilling' appears with only 1 click! – LopezLink. Jan 28, 2010 - iWantv! gives you a chance to get well known shows in video-on-request and live channel groups. The video-on-request choice makes live gushing of iWant television 2.3.2 for Android – Download Dialect. Creator: ABS-CBN. Security level: 100% safe. Estimate: 3.04MB. Downloads: 12,463. Date: 07.29.16. Bundle Name: com.abscbn.iwantv. IWanTV | LoadCentral Philippines Its administrations incorporate Article source and voice, and portable web alongside the restrictive access to ABS-CBN substance through the IWanTV! application. It was propelled on Article source
ABS-CBN Shows, iWanTV, different substance to be accessible to Shrewd. Sep 27, 2016 - To begin with iWant television, PLDT Home and Savvy endorsers can enlist for a record by means of www.iwantv.com.ph or download the iWant television. iWantv!: Online Free television at your own opportunity | penworks | photoblog Nov 8, 2010 - iWantv is an administration allowed to all SKYBROADBAND and BayanDSL endorsers. Facilitating all the top of the line shows of ABS-CBN, ANC, Studio 23. Get Premium Access on iWantTV with Article source 50! | Starmometer. Nov 13, 2016 - Missing a scene of your most loved primetime teleserye in light of terrible movement is presently a thing previously. With ABS-CBN's iWanTV, I can.
Interpret this page. Liza Soberano and Enrique Gil repeat their very much cherished parts as Agnes and Xander as they top bill "Everlastingly and the sky is the limit from there," the main select substance offering on. the most effective method to download recordings from iwantv » YugaTech | Philippines Tech. BayanDSL and Article source is currently offering live gushing television and video-on-request to all it's DSL endorsers and Sky Broadband clients. Accessible channels incorporate. iWanTV Files – LIONHEARTV. More on the web and cell phone clients keep on watching ABS-CBN programs online by means of the video-on-request and livestreaming administration iWanTV. Article source
هل تعانون من الحشرات وكثرتها واضرارها البالغة نقدم لكم افضل شركة لابادة جميع انواع الحشرات بالضمان وهي شركة المثالية لمكافحة الحشرات حيث تقدم اليوم افضل العروض والخدمات بارخص الاسعار بخصم 50% لفترة محدودة
شركة المثالية لمكافحة الحشرات
شركة المثالية لمكافحة الحشرات بالدمام
شركة المثالية لمكافحة الحشرات بالخبر
شركة المثالية لمكافحة الحشرات بالقطيف
______________________________________________________________________________________________________________
شركة المثالية لمكافحة الحشرات بالاحساء
شركة المثالية لمكافحة الحشرات بالجبيل
شركة المثالية لمكافحة الحشرات بجدة
شركة امست للتنظيف افضل شركة لتنظيف المنازل والشقق والفللوالبيوت والمجالس والكنب والمفروشات والخزانات بالقطيف والمنطقة الشرقية لديها عمالة مدربة وخصومات هائلة طما توفر خدمات مكافحة الحشرات ورش المبيدات بالقطيف باسعار رخيصة ومناسبة وخصومات هائلة
شركة امست للتنظيف
امست للنظافة القطيف
شركة امست لمكافحة الحشرات
شركة امست لرش المبيدات
مغسلة الجبر
مغاسل الجبر للتنظيف
مغاسل الجبر بالرياض
مغسلة الجبر للتنظيف
شركة تنظيف كنب بالقطيف
شركة تنظيف مكيفات بالقطيف
شركة تنظيف مطابخ بالقطيف
شركة مكافحة حشرات بالقطيف
شركة مكافحة النمل الابيض بالقطيف
افضل شركة تنظيف بالقطيف تقدم خدمات نظافة متكاملة علي اعلي مستوي بدقة وجودة ومواصفات قياسية واسعار تنافسية فتعد الشركة الاولي بالقطيف لتقديم خدماتالتنظيف بجودة وسعر مثالييينفهي خير مثال لراغبي الحصول علي نظافة شاملة مع التعقيم وباسعار مناسبة
شركة تنظيف بالقطيف
شركة تنظيف منازل بالقطيف
______________________________________________________________________________________________________________________
شركة كشف تسربات المياه بالقطيف
شركة تنظيف مجالس بالقطيف
شركة تنظيف خزانات بالقطيف
شركة شفط بيارات بالقطيف
شركة المثالية للتنظيف افضل شركة تنظيف بالمنطقة الشرقية تخصصت في تقديم كافة الاعمال المنزية من نظافة ومكافحة حشرات بالمنطقة فلديها افضل الخدماتبافضل جودة واوبارخص الاسعار التي لا تتوفر الا معها كافضل وارخص شركة تنظيف بالاضافة الي خدمتا مكافحة الحشرات ورش المبيدات الامنة والفعالة علي الحشرات بجميع انواعها .. تقدم لكم اليوم افضل العروض والخدمات بخصومات هائلة يمكنكم الحصول عليها من خلال الروابط التالية
شركة المثالية للتنظيف
شركة المثالية للنظافة
شركة المثالية للتنظيف بالدمام
شركة المثالية للتنظيف بالخبر
شركة المثالية للتنظيف بالقطيف
شركة ماسة لمكافحة الحشرات
شركة ماسة لرش المبيدات
شركة ماسة لمكافحة الحشرات بالاحساء
شركة ماسة لمكافحة الحشرات بالرياض
شركة طيوب لتسليك المجاري
شركة طيوب لتسليك المجاري بالقطيف
شركة طيوب لتسليك المجاري بالدمام
شركة طيوب لتسليك المجاري بالخبر
شركة طيوب لتسليك المجاري بالاحساء
شركة ورس لمكافحة الحشرات
شركة ورس لرش المبيدات
شركة ورس لمكافحة الحشرات بالاحساء
Post a Comment