{Dev Tricks} » Code http://dev-tricks.net Blogging developper tips and tricks Sun, 30 Aug 2009 00:14:47 +0000 http://wordpress.org/?v=2.9.2 en hourly 1 Post data lost on 301 Moved Permanently http://dev-tricks.net/post-data-lost-on-301-moved-permanently http://dev-tricks.net/post-data-lost-on-301-moved-permanently#comments Fri, 15 May 2009 10:50:05 +0000 Julien Palard http://dev-tricks.net/?p=227 Got the page, 200 OK or http://example.com/directory/ -> Got the page, 200 OK or http://example.com/directory -> Got a 301 [...]]]> What the hell with 301 Moved Permanently HTTP header !?

I’ll take an exemple to explain my ugly problem, take, a [very ugly cause it's PHP] index.php, it could be retrieved by using :

http://example.com/directory/index.php

-> Got the page, 200 OK
or http://example.com/directory/
-> Got the page, 200 OK
or http://example.com/directory
-> Got a 301 Moved Permanently Location: http://example.com/directory/

Just like expected … but…

As you can see, on the captured HTTP headers below, when you POST data on a 301 target, you’ll be redirected, but unfortunately you’ll lost your POST data, even worse, your request can be reforged as a GET request !


Let me show you an example :
Request :

POST /directory HTTP/1.1
Host: example.com
[user agent, referer, cache control, origin, content type, accept, accept-encoding, blah blah ...]
Content-Length: 7

foo=bar

Response :

HTTP/1.0 301 Moved Permanently
Location: http://example.com/directory/
[... blah blah]

Reforged request :

GET /directory/ HTTP/1.1
Host: example.com

Why my data isn’t kept in the reforged one !?

So let’s read The RFC 2616 (about HTTP …)

10.3.2 301 Moved Permanently

[...]

The new permanent URI SHOULD be given by the Location field in the
response. Unless the request method was HEAD, the entity of the
response SHOULD contain a short hypertext note with a hyperlink to
the new URI(s).

If the 301 status code is received in response to a request other
than GET or HEAD, the user agent MUST NOT automatically redirect the
request unless it can be confirmed by the user, since this might
change the conditions under which the request was issued.

Note: When automatically redirecting a POST request after
receiving a 301 status code, some existing HTTP/1.0 user agents
will erroneously change it into a GET request.

Ahh, ok, so, for request other than GET or HEAD, the user agent MUST NOT automatically redirect to the request ? Ugh, it’s not the case in IE 6, 7, 8, Firefox, nor Chrome.
And, about the security, while the server recieves the data, it can do everything, for example send it to another server directly.
Then I’m wondering, why asking to the user if he allows his data to be transfered ?

Then, the RFC says an interesting thing …

Note: When automatically redirecting a POST request after
receiving a 301 status code, some existing HTTP/1.0 user agents
will erroneously change it into a GET request.

But, not only HTTP/1.0 user agents, but HTTP/1.1 user agents like browsers and frameworks (as, I encountred the problem firstly in the .NET Framework 3.5, in HttpWebRequest) )o:

OK, end of this article, have a nice day )o’,:

]]>
http://dev-tricks.net/post-data-lost-on-301-moved-permanently/feed 0
Integrating google bookmarks in google chrome http://dev-tricks.net/integrating-google-bookmarks-in-google-chrome http://dev-tricks.net/integrating-google-bookmarks-in-google-chrome#comments Thu, 16 Apr 2009 15:56:18 +0000 Julien Palard http://dev-tricks.net/?p=223 base tab -> Default search engine -> Manage Click “Add” and fill [...]]]> As every developer, you have 42 computers, 8 browsers, and spent a lot of time asking why google chrome does not integrate google bookmarks ?

Here is a solution :
First add the “Add to Google Bookmarks” bookmarklet found here
http://www.google.com/support/chrome/bin/answer.py?hl=en&answer=100215
And then go to chrome’s options -> base tab -> Default search engine -> Manage
Click “Add” and fill :
Name : Google Bookmarks
Keyword : The keyword you wanna enter before a bookmark search in the chrome’s address bar (to search 42, if your keyword is, like mine ‘b’, type “b 42″ in the chrome’s address bar and 42 will be searched in your bookmarks)
URL : http://www.google.com/bookmarks/find?q=%s

Enjoy !

]]>
http://dev-tricks.net/integrating-google-bookmarks-in-google-chrome/feed 0
C# Using alias directives http://dev-tricks.net/c-sharp-using-alias-directives http://dev-tricks.net/c-sharp-using-alias-directives#comments Wed, 25 Mar 2009 09:49:50 +0000 Julien Palard http://dev-tricks.net/?p=218 Just found in section 9.4.1 of the C# language specification :
The using keyword can be used to alias a namespace or a type name :

using-alias-directive:
using identifier = namespace-or-type-name ;


You can read more about that here : csharp language specification.doc

Or just try to use it :

Unable to get source code, check your URL given in [viewcode].

Download this code: ex.cs

Enjoy !

]]>
http://dev-tricks.net/c-sharp-using-alias-directives/feed 1
Covariance and Contravariance in C#3 http://dev-tricks.net/covariance-and-contravariance-in-c-sharp-3 http://dev-tricks.net/covariance-and-contravariance-in-c-sharp-3#comments Mon, 09 Mar 2009 19:19:51 +0000 Julien Palard http://dev-tricks.net/?p=203 A short introduction to Covariance and Contravariance in C# 3 preparing you to an article about that in C# 4.
So what is covariance ?
Covariance is basically using a method which returns something derived from the expected type.

An exemple ? It’s safe to have a method returning a cat when you expect it to return an animal.

In C sharp it’s

Unable to get source code, check your URL given in [viewcode].

Download this code: covariance.cs

So Funcs a, b, and c are returning animals which in fact are cats, which is true.

And, what is contravariance ?
Contravariance is basically using a method which takes something which is a parent of the expected type.
An exemple ? It’s safe to have a method taking an animal when you expect it to take a cat

Unable to get source code, check your URL given in [viewcode].

Download this code: contravariance.cs

So Action a take Cats, but in fact can take any Animals, so it’s safe.

]]>
http://dev-tricks.net/covariance-and-contravariance-in-c-sharp-3/feed 1
Silverlight Tutorial – Flashlight Tutorial http://dev-tricks.net/silverlight-flashlight-tutorial http://dev-tricks.net/silverlight-flashlight-tutorial#comments Mon, 19 Jan 2009 20:48:13 +0000 Antoine Angot http://dev-tricks.net/?p=80 How to make a flashlight effect with silverlight ?
 
 

 
 

    What you need :
     

  • be familiar with Blend
  • be familiar with Visual Studio
  • half an hour
     
     
     

    Introduction

     
    Many years ago, a friend taught me how to make a flash light effect with flash.
    You probably saw it in many tutorials.
     
     

     
    Today, we will try to do better with silverlight :)

     
     
     

    The Blend Part

     

      Create a new project

     
    I’m doing it with Blend, but you can use Visual Studio if you want.

      I’m doing it with Blend, but you can use Visual Studio if you want.

     
     

      Add an image

     

      Add it to the project.

     
     

      Resize the image to give it the same size that the LayoutRoot

     
     

      Draw a rectangle

     
     

      select the tool

     
     

      You should get this.

     
     

      Gradient Brush and Radial Gradient

     
     

      Set up the rectangle background as a gradientbrush

     
     

      And click on the radial gradient option

     
     

      You rectangle should look like this.

     
     

      Transparency

     
     

      Set the first gradiant to black

     
     

      For the other one, same color but set transparenrcy to half

     
     

      We begin to see the final result
       
       

      Draw your Flashlight

     
     

      Play with gradiant and try to make something which looks like a light.

     
     

      Here is the artistical part :)

     
     

      Well, i think i’m fine.

     
     

      For me, it looks like this.

     
     

      Transform the Brush

     
     
    if you want to be able to move, you have to enlarge the brush.
     

      Select the “Brush Transform” tool.

     
     

      Resize the brush to about one and half.

     
     
     
     

    The Visual Studio part

     

     

    Ok this part is ridiculous you have to add a method in FlashLight.xaml.cs to register mouse move.
     

    Unable to get source code, check your URL given in [viewcode].

    Download this code: FlashLight.cs

     
     
     

    Conclusion

     
     
    Actually, i’m pretty sure that you can do the same with the last version of Flash but this tutorial is a good exercise to practice with Blend.
    Thanks for reading. You can download the source code here.
     
     

]]>
http://dev-tricks.net/silverlight-flashlight-tutorial/feed 0
Silverlight Custom Control – II : Properties http://dev-tricks.net/silverlight-custom-control-properties http://dev-tricks.net/silverlight-custom-control-properties#comments Thu, 15 Jan 2009 10:14:44 +0000 Julien Palard http://dev-tricks.net/?p=78 This is the second part of my serie about creating Custom Controls, if you missed the first, you should read it : Part I

In this Part, we’ll speak about Custom Control’s properties : How to databind a template property to an exposed property, and how to create new exposed properties.


The idea is to expose properties of your custom control to its users, as your custom control is inheriting another, its basically exposes the sames, in the previous article, we got
public class EditableTextBlock : Control
so our EditableTextBlock exposes Width, Height, Background …

But setting EditableTextBlock’s Background to Red does not affects the Grid’s Background Property (setted to AliceBlue) in the Template (generic.xaml), you can try it :

Unable to get source code, check your URL given in [viewcode].

Download this code: Page.part.xaml

The control stay in AliceBlue …

At this point you can use some DataBinding to link the value of a property in your control template to the value of some exposed property of its parent, to do this, in generic.xaml, the Grid’s Background property must be Binded to the exposed Background Property :

Unable to get source code, check your URL given in [viewcode].

Download this code: generic.xaml

Now, the Grid’s Background is binded to the exposed Background !

How to create a new property ?
You have to use a DependencyProperty to expose your property :
Basically, a DependencyPorperty look like this :

Unable to get source code, check your URL given in [viewcode].

Download this code: DependencyProperty.cs

So a Text Property looks like this :

Unable to get source code, check your URL given in [viewcode].

Download this code: TextProperty.cs

The EditableTextBlock control now exposes a Text property which can be Binded in your Template.

]]>
http://dev-tricks.net/silverlight-custom-control-properties/feed 0
Silverlight Custom Control – I http://dev-tricks.net/silverlight-custom-control-i http://dev-tricks.net/silverlight-custom-control-i#comments Wed, 14 Jan 2009 16:31:49 +0000 Julien Palard http://dev-tricks.net/?p=76 This article is my first about creating Custom Controls in Silverlight 2.
So let’s build a simple one :
You need :

  • A .cs file, named with your new Custom Control name
  • A directory named Themes
  • A generic.xaml file in it (just delete generic.xaml.cs if it was created)

Silverlight Custom Control Tree in Visual Studio

A basic custom control .cs file (the logic part):

Unable to get source code, check your URL given in [viewcode].

Download this code: EditableTextBlock.cs

A basic custom control generic.xaml file (the graphic part):

Unable to get source code, check your URL given in [viewcode].

Download this code: generic.xaml

Now, to test, you need to add the xmlns of your namespace, and add the usercontrol :

Unable to get source code, check your URL given in [viewcode].

Download this code: page.xaml

In the next article, we’ll see how to bind properties with TemplateBinding.
Don’t forgot to read the MSDN !

]]>
http://dev-tricks.net/silverlight-custom-control-i/feed 1
Combinatory logic from scratch http://dev-tricks.net/combinatory-logic-from-scratch http://dev-tricks.net/combinatory-logic-from-scratch#comments Thu, 27 Nov 2008 23:56:32 +0000 Julien Palard http://dev-tricks.net/?p=64 Cause it’s sooooo sexy, let’s speak about Combinatory Logic !
Rule 1 : You don’t talk about Combinatory Logic
Rule 2 : You don’t talk about Combinatory Logic
Rule 3 : Combinatory Logic is based on Lambda Calculus (see Wikipedia for both)
Rule 4 : A combinator is a Lambda expression taking One and only One combinator as parameter, and returning a Combinator.


As i’m speaking to developers, i’ll use the C# Lambda syntax which is :
(parameter) => statement

Let’s now try our first Combinator, named the Identity Combinator
I = (a) => a;
I named it I, it takes one parameter, localy named ‘a’ and return the parameter as is.

Important Point : How to build combinator taking more than one parameter ?
In C# you should use (a, b, c) => blah blah… but the Rule 4 forbid us to give more than one paraneter, so let’s cheat, imagine :
K = (x) => (y) => x;
K is a Combinator taking x, returning a Combinator taking y and returning x.
so we have K(x) = (y) => x and K(x)(y) = x !
So K take two arguments, x and y, and returns x, but ! K can take only one argument, look at the “K(x) = (y) => x” …

Let’s try with three arguments :

S = (x) => (y) => (z) => x(z)(y(z))
can be called with one, two, or three arguments :
S(x) returns (y) => (z) => x(z)(y(z))
S(x)(y) returns (z) => x(z)(y(z))
s(x)(y)(z) returns x(z)(y(z))

In combinatory logic, they wrote :
I a = a
K x y = x
S x y z = x(z)(y(z))

then they say that in fact, I can be build from S and K :
I = SKK
Ok but what does it means ? where are arguments ? it’s easy :

I = S(K)(K);
S can take 2 parameters “S(x)(y) returns (z) => x(z)(y(z))” ok ? so :
I = (z) => K(z)(K(z))
We have to execute it from left to right, remember, K(a)(b) returns a, so (with a == z and b == K(z)) :
I = (z) => z;

Do you want more ?

Let’s try to understand
B = S (K S) K x y z
B stands for Barbara, from “Syllogism Barbara” (wikipedia says :)
Barbara :
All men are animals.
All animals are mortal.
All men are mortal.

So before all, write B as we understand it, and for readability reasons, i’ll underline parameters for the bolded combinator :
B = S (K(S)) K (x) (y) (z)
We have to execute it from left to right, and we have a S with three parameters :
S(a)(b)(c) returns a(c)(b(c)) :
B = K (S) (x) (K(x)) (y) (z)
From left to right we have a K with two parameters, S and y, it will return S :
B = S (K(x)) (y) (z)
Calling S with three parameters (K(x)), (y), (z) returns (K(x))(z)((y)(z)) :
B = K (x) (z) ((y)(z))
Calling K with two parameters (x), (z), it returns x :
B = x((y)(z))
Which can be simplified to :
B = x(y(z))

It’s time to try it !

Unable to get source code, check your URL given in [viewcode].

Download this code: test.cs

It works ! Enjoy ! ! Next time, we will try a Swap combinator, a Combinator reducing to himself and progressing step to the Y Combinator !
[dramatic chord]

]]>
http://dev-tricks.net/combinatory-logic-from-scratch/feed 0
The ?? operator aka the Null Coalescing Operator http://dev-tricks.net/the-operator-aka-null-coalescing-operator http://dev-tricks.net/the-operator-aka-null-coalescing-operator#comments Sun, 23 Nov 2008 16:01:40 +0000 valerian http://dev-tricks.net/?p=61 If are familiar to the use of ternary operators, you must have encountered several situations like this one :

Unable to get source code, check your URL given in [viewcode].

Download this code: bad_situation.cs

You would want to call getTitle() only once, but then you wouldn’t have a simple one-lined initialization of you variable.

Actually there is a simple solution that most langages implements, the null coalescing operator; let’s replace the above C# code with this one :

Unable to get source code, check your URL given in [viewcode].

Download this code: solution.cs

Now you have the same behaviour, but with only one call to your function. The syntax is simple :

possibly_null_value ?? value_if_null

the “??” operator is implemented in C# since C# 2.0.

You also have it in C and C++ as a GNU extension using the “?:” operator :

Unable to get source code, check your URL given in [viewcode].

Download this code: solution.cpp

You can get the same behaviour in javascript using the “||” operator :

Unable to get source code, check your URL given in [viewcode].

Download this code: solution.js

You can read more about this operator here and here

]]>
http://dev-tricks.net/the-operator-aka-null-coalescing-operator/feed 0
Lazy Loading in php with spl_autload http://dev-tricks.net/lazy-loading-in-php-with-spl_autload http://dev-tricks.net/lazy-loading-in-php-with-spl_autload#comments Sun, 16 Nov 2008 18:40:44 +0000 Julien Palard http://dev-tricks.net/?p=51 Today, a very short post about the lazy loading in PHP : spl_autoload
I’ll not expose everything about it here, cause it’s already done here :
http://php.net/autoload

Basically, a Lazy Loading allow you to predifine some paths where PHP should seek for classes to include, this allow you to directly instantiate an object without having included its file.

public static function lazyLoad($strClassName)
{
        /* some work to find the file to include mixing strClassName */

        /* include your files here... */
}
spl_autoload_register("lazyLoad");

Download this code: lazy.php

]]>
http://dev-tricks.net/lazy-loading-in-php-with-spl_autload/feed 0