Tuesday, October 30, 2012

iOS 6 Ajax bug

As you may have read, there are some bugs in iOS 6 related to Ajax. This post explains it well. I had problems getting the " $.ajaxSetup " option to work, even though I didn't spend much time diagnosing it. Same with " $.ajaxPrefilter " ( see this post )
  While it appears that in Apache you can set the "Pragma: no-cache" just for posts on the server side, in IIS the http headers apply to all methods.
  I opted for creating a specific HTTP module that sets headers to disable client caching for certain specific requests. Here is the C# code:

    using System;
    using System.Web;
    using System.Text.RegularExpressions;


    /// HttpModule responsible for setting headers to disable cache in case of posts from iPad+iOS6+Safari. This environment may cache Ajax posts. 
    public class HttpNoCacheModule : IHttpModule
        // user agent regular expression to capture Safari on iPad iOS 6
        private Regex ios6Regex = new Regex(@"iPad.+OS 6.+Safari", RegexOptions.IgnoreCase);

        public void Dispose()

        public void Init(HttpApplication app)
            app.EndRequest += new EventHandler(this.OnEndRequest);            

        private void OnEndRequest(Object source, EventArgs e)
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            if (ios6Regex.IsMatch(context.Request.UserAgent))
                if ("POST".Equals(context.Request.HttpMethod))
                    context.Response.AddHeader("pragma", "no-cache");
                    context.Response.CacheControl = "no-cache";


Tuesday, October 16, 2012

Perguntas Do Dia

É verdade que só os Evangélicos vão para o céu? Explique-me isso.
É verdade que apenas uma denominação está certa e detém a Verdade, a saber, a sua? Explique-me isso.
É verdade que tem algumas religiões que são essencialmente más? Explique-me isso.

Thursday, October 04, 2012

Powershell to generate Delicious graph of tags

Here is a Powershell script that generates a graph in GEXF format of my Delicious tags. GEXF is an XML representation of a graph that may be consumed by Sigma.js, a JavaScript library for rendering graphs.

Set-StrictMode -version 1
Function CreateNode($id, $label, $xmlDoc, $nodes, $rootNS, $ZeroAttributeValue, $OneAttributeValue, $comment)
      $node = $xmlDoc.CreateElement("node", $rootNS)
      if ($ZeroAttributeValue) { # occurrences
          $attvalues = $xmlDoc.CreateElement("attvalues", $rootNS)
          $attvalue = $xmlDoc.CreateElement("attvalue", $rootNS)
      if ($OneAttributeValue) { # URL
          $attvalues = $xmlDoc.CreateElement("attvalues", $rootNS)
          $attvalue = $xmlDoc.CreateElement("attvalue", $rootNS)

Function CreateEdge($id, $source, $target, $xmlDoc, $edges, $rootNS)
       $edge = $xmlDoc.CreateElement("edge", $rootNS)
Function CreateAttributes($xmlDoc, $graph, $rootNS)
    $attribs = $xmlDoc.CreateElement("attributes", $rootNS)
    $attrib = $xmlDoc.CreateElement("attribute", $rootNS)
    $attrib = $xmlDoc.CreateElement("attribute", $rootNS)
Function EqualString($str1, $str2) {
   return !$str1.CompareTo($str2)
Function WhiteListTag($tag) {
  $lang = !'português'.CompareTo($tag) -or !'español'.CompareTo($tag)
  $black = (EqualString 'CSN_' $tag) -or (EqualString 'CSN_source' $tag)  -or (EqualString 'CSN_f' $tag) -or (EqualString 'CSN_t_' $tag) -or (EqualString 'CSN_freepage' $tag)
  $hasPrefix = $tag.StartsWith('CSN_')
  return $lang -or ( !$black -and $hasPrefix )
Add-Type -AssemblyName "System.Net"
$json = ""
Get-Content C:\Users\gustavo.frederico\Documents\jsonDeliciousGF.txt |foreach{$json += $_ + "`r`n"}
$rss = [Newtonsoft.Json.Linq.JObject]::Parse($json)
[xml]$xmlDoc = New-Object system.Xml.XmlDocument
[System.Xml.XmlNamespaceManager] $nsmgr = $xmlDoc.NameTable
$rootNS = $xmlDoc.DocumentElement.NamespaceURI
$meta = $xmlDoc.CreateElement("meta", $rootNS)
$creator = $xmlDoc.CreateElement("creator", $rootNS)
$creator.AppendChild($xmlDoc.CreateTextNode('CSNombre script'));
$desc = $xmlDoc.CreateElement("description", $rootNS)
$desc.AppendChild($xmlDoc.CreateTextNode('gexf representation of Delicious entries'));
$graph = $xmlDoc.CreateElement("graph", $rootNS)
CreateAttributes $xmlDoc $graph $rootNS
$nodes = $xmlDoc.CreateElement("nodes", $rootNS)
$csnProps = $rss.Properties() | where { WhiteListTag $_.Name.ToString() }
$existingLabelNodesTags = @{}
# key: tag name , value: tag node id
$existingLabelNodesURLs = @{}
#  key: URL , value: url node id.
$tagNodeId = 0
$edgeid = 0
foreach($prop in $csnProps) {
   $existingLabelNodesTags.Add($prop.Name.ToString(), $tagNodeId)
   CreateNode $tagNodeId $prop.Name.ToString() $xmlDoc $nodes $rootNS $prop.Value.ToString() $null 'tag/label node'
#   Write-Host $prop.Value.ToString()
$wc = New-Object System.Net.WebClient
$edges = $xmlDoc.CreateElement("edges", $rootNS)
$URLNodeId = $tagNodeId
foreach($prop2 in $csnProps) {
   $currentTag = $prop2.Name.ToString()
   $tagNodeIdForCurrentTag = $existingLabelNodesTags.Get_Item($currentTag)
   $rawJson = $wc.DownloadString("http://feeds.delicious.com/v2/json/gcsfred/" + $currentTag) # "?count=99"
   $rawJson = "{ array: " + $rawJson + " }"
   $references = [Newtonsoft.Json.Linq.JObject]::Parse($rawJson)
   $referencesProps = $references.Properties()
   foreach($refProp in $references['array']) {  
       $uVal = $refProp['u'].ToString()
       $dVal = $refProp['d'].ToString()
       # create node for u  , d if it doesn't exist
       if ( $existingLabelNodesURLs.ContainsValue($uVal) ) {      
          $nodeIdURL = $existingLabelNodesURLs.Get_Item($uVal)
       } else {
          $existingLabelNodesURLs.Set_Item($uVal, $URLNodeId)
          $nodeIdURL = $URLNodeId        
          # create node for u and d
          CreateNode $nodeIdURL $dVal $xmlDoc $nodes $rootNS $null $uVal 'URL node'
       # now, create edge between currentTag node and URL nodes
       CreateEdge $edgeid $tagNodeIdForCurrentTag $nodeIdURL $xmlDoc $edges $rootNS
       # end of u and d
       $otherTags = $refProp['t']
       while( $i -lt $otherTags.Count) {
         $otherTag = $otherTags[$i].ToString()
         $isWhite = WhiteListTag $otherTag
         if (!$isWhite) {
         # fetch node id of $otherTag
         $nodeIdOtherTag = $existingLabelNodesTags.Get_Item($otherTag)
         CreateEdge $edgeid $nodeIdURL $nodeIdOtherTag $xmlDoc $edges $rootNS      
   # end for each reference tag