Saturday, December 5, 2009

我们应该感谢党啊

我们应该感谢政府,感谢党。是他们,让我们学会了因特网的基本协议,了解了网络安全的重要性,知道了什么叫域名劫持,提高了中国网民的整体素质,他们不再只是用电脑看看电影,上上黄网。更重要的是,他们知道了什么是VPN ,DNS ,SSL ,HTTPS。他们能熟练的使用Tor,Wujie,Freegate,甚至使得某宗教人士摇身一变,成为了IT界人士。而且我们还成为了业余的武器制造专家,没事还可以做点燃烧弹什么的。以后可以从出口纽扣,衬衫什么的,转变为出口点军火。更重要的是,还增加了许多人的法律知识,许多人因此而熟悉《宪法》《物权法》《民法》《宪法》。。。

今天见到了“民间科学家”了

今天去上海图书馆,在门口看到一群人围观一个老人,我的第一个反应就是,可能又是什么六月飞雪的冤情。我在人民广场上看到过,在一号线上碰到过“胖老师”,甚至在交大的自习室也见过“胖老师”,可在图书馆的门口,有点匪夷所思,除非和图书馆有什么关系。走近一看,恍然大悟,只见这位老人拿着两张A4大小的纸,密密麻麻的写着什么,我也没有走近看。但是在纸的最上头有大号字体写着“数学悖论”,中间写着“寻师访友,绝对真实”。这位老人还激动的向围观的不明真相的群众用上海话叙述着什么。我没听懂他在讲什么,也没走近看他究竟写了什么,但是看到“数学悖论”这四个字,我到脑海中立马出现那些给中科院数学所寄整袋稿纸,声称解决了哥德巴赫猜想的那些人。或是,数学悖论并不是数学悖论。

Sunday, November 29, 2009

How Eulder Did it



Euler, you are so amazing. You are not human being. Are you ET or is there any gods on you? The theorems and proofs you provided two hundred years ago are so elegant, they are the outstanding work only God can make. Anyway, you are not alone. You are one of the brightest stars in the history of human.

Saturday, November 28, 2009

Discworld·Sourcery

    特里·普拉切特(1948-),英国著名幻想小说家,有“幻想小说家超级巨星”之称。




 普拉切特自一九八三年开始创作供成人阅读的“碟形世界” (TheDiscworld)系列,以讽刺、幽默的笔调塑造了一个神奇却又真实的幻想世界,目前已经完成三十多本,但故事还在继续……该系列已被翻译成三十多种文字,作者本人也因此获得“英国最佳幻想小说奖”,并因其“为文学做出的贡献”于一九九八年在英国女王的生日庆典上被授予英帝国的“四等勋爵士”称号。




 特里·普拉切特是当代最著名的幽默奇幻作家,同时也是英语文坛最具影响力的讽刺作家之一。被人誉为“笔锋犀利、擅于讽刺的J·R·R·托尔金”。深受读者推崇的“碟形世界”系列奇幻小说是普拉切特的代表作品,为他赢得了世界声誉。该系列从1983年《魔法的颜色》问世算起,到2006年,共出版了36部长篇小说,其中多部被改编成了漫画、动画、舞台剧、电视剧、广播剧、桌面游戏和电脑游戏等。截至2007年2月,普拉切特的作品在全球累计销售5000万册。现在大陆出版了的只有四本:〈魔法的色彩〉〈异光〉〈平等权利〉〈死神学徒〉(来自百度百科)

    新的一期《科幻世界·译文版》的长篇小说是《碟形世界·大法》


Tuesday, November 24, 2009

敏感词


QQ新头像.

SharePoint AllowUnsafeUpdates

 Today I meet a problem related with the AllowUnsafeUpdates. I found there is a greate artical about it. http://hristopavlov.wordpress.com/2008/05/16/what-you-need-to-know-about-allowunsafeupdates/

In short here is how to deal with AllowUnsafeUpdates:
1) Don’t update SharePoint objects from your code behind on GET requests as if you do so your code will be exploitable via a cross-site scripting. If you understand the consequences of doing this and still want to do it then read below about how to use the AllowUnsafeUpdates property.
2) If your code is processing a POST request then make sure you call SPUtility.ValidateFormDigest() before you do anything else. This will ensure that the post request is validated (that it is not a cross-site scripting attack) and after that you will not have to worry about AllowUnsafeUpdates, because its default value will be “true” after the form digest is validated. To find out more about this read the second part of this article.

The Microsoft idea behind introducing the AllowUnsafeUpdates property is to protect YOU from cross-site scripting attacks. The way this works is that if your application is running in an HTTPContext (i.e. it’s a web part for instance) and the request is a GET request then SharePoint will refuse to do any changes unless the value of AllowUnsafeUpdates is set to true and by default it will be false for GET requests. If you try to do any updates to lists, webs or any SharePoint objects that require an SPSite to be created first, and if you don’t set AllowUnsafeUpdates to true you will get this exception:
System.Exception: Microsoft.SharePoint.SPException: The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again. —> System.Runtime.InteropServices.COMException (0×8102006D): The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.
It is important to understand that if you are writing a class library for example, your code will behave differently when called from a web application and when called from a rich client. Actually if the HTTPContext.Current is null then AllowSafeUpdates will be always true. This is the case in rich clients where no cross-scripting is possible as there are simply no web requests.
Usually when you create your own SPSite or SPWeb objects, i.e. when you are not getting them from the SPContext (such as SPContext.Web), and when you try to update anything such as web or list properties, list items metadata etc, you may get the exception listed above. This is a clear indication that AllowUnsafeUpdates of the SPWeb is false and this is preventing you from doing the update. This problem is resolved easily by setting the AllowUnsafeUpdates of the parent web object to true. Still sometimes even after you have done this you may still be getting the same error.  This is typically caused by one of the the following reasons:
A) You have set the AllowUnsafeUpdate to true for the wrong SPWeb
You have to be careful because sometimes the ParentWeb of an object is not the same instance of the web you have retrieved the object from. For example when you go initialWeb.Lists[listId] you would expect that the returned list’s ParentWeb instance is the same as you initialWeb. However this is not the case. So if somewhere later in your code you go list.ParentWeb.UpdateSomething() this will not work because you have never set the AllowUnsafeUpdates property of list.ParentWeb. You have set it for your initialWeb but even that this is the same web as the list’s parent web both are different instances. Usually you see the error and then you go and investigate in Reflector whether this is the same instance or not. Alternatively you could use another more generic and clever way to deal with almost any similar situation described in the following post:
http://community.bamboosolutions.com/blogs/bambooteamblog/archive/2008/05/15/when-allowunsafeupdates-doesn-t-work.aspx
The author suggests that you can set the HttpContent.Current to null before you do your updates and then reassign its initial preserved value when done. This will work great but remember to set the HTTPContent to null as early as possible. In the post above probably SharePoint uses the site.RootWeb to do the updates to the site scoped features and the RootWeb’s AllowUnsafeUpdates hasn’t been set to true explicitly.
B) The AllowUnsafeUpdates gets reset to false sometimes after you have set it to true
If we have a look at how the property is managed it turns out that it is stored in the request object associated with every SPWeb (which is actually a COM object)

[SharePointPermission(SecurityAction.Demand, UnsafeSaveOnGet = true)]
private void SetAllowUnsafeUpdates(bool allowUnsafeUpdates)
{
     this.Request.SetIgnoreCanary(allowUnsafeUpdates);
}
This actually means that every time the request is reset, the property will be also reset to its default value. The m_Request member is modified when a new web is created, when the web is disposed or when the SPWeb.Invalidate() method is called.

internal void Invalidate()
{
   if (this.m_Request != null)
   {
      if (this.m_RequestOwnedByThisWeb)
      {
         SPRequestManager.Release(this.m_Request);
      }

      this.m_Request = null;
   }

   this.m_bInited = false;
   this.m_bPublicPropertiesInited = false;
   this.m_Url = null;
}
So any operation that calls SPWeb.Invalidate() will reset AllowUnsafeUpdate to its default value. And for code running under HTTPContext, i.e. web applications, this default value for a GET request will be false. I’ve looked up for you all legitimate cases for which Invalidate() is being called by the SharePoint object model. These cases are:
1) When the Name or the ServerRelativeUrl properties of the SPWeb are changed and then Update() is called. In this case the AllowUnsafeUpdate is reset because with the change of these properties the URL of the web will change and logically the request object will change as it will now point to a different URL.
2) When any object that implements ISecurable (those are SPWeb, SPList and SPListItem) breaks or reverts their role definition inheritance. This means every time you call SPRoleDefinitionCollection.BreakInheritance(), BreakRoleInheritance(), ResetRoleInheritance() or set the value of HasUniquePerm the AllowUnsafeUpdates property of the parent web will reset to its default value and you may need to set it back to true in order to do further updates to the same objects.
3) In many cases when an exception is caught by the SharePoint object model when you try to retrieve any sort of data the AllowUnsafeUpdates of the parent web will be reset to false as a precaution to protect against potential exploits. In those cases however the objects will be in unknown state anyway after the request has been reset and the exception is re-thrown so they are of no practical interest.
And finally it is also good to mention that you may get another related exception when trying to update your SharePoint objects and that is:
System.Exception: Microsoft.SharePoint.SPException: Cannot complete this action.Please try again. —> System.Runtime.InteropServices.COMException (0×80004005): Cannot complete this action.
This usually happens when some updates have been made to an object (usually SPSite, SPWeb or SPList) that may be clashing with your changes and SharePoint refuses to do the update. To recover from this situation you simply need to create fresh copies of the SPSite and the SPWeb objects and do the updates on the objects retrieved from the fresh copies. And of course don’t forget to set the AllowUnsafeUpdates to true for the freshly created SPWeb if required.

Sunday, November 22, 2009

手机的悲剧


前天跟哈密瓜同学吃饭,期间提到了手机的问题。她说要去买个新的手机,我给予了相应的回应。结果第二天早上一起来,在我睡眼惺忪的时候,想看看地球上的时间,发现我那英俊潇洒,任劳任怨,意志坚强,思想正确,一直默默无闻工作的Nokia 6120c终于抵挡不住滚滚经济大潮的席卷,抵御不住资产阶级自由化的腐蚀,愤然罢工了。在送往工会研究学习一番,工会领导语重心长的对我说,要要交218的学习费用,给它洗洗脑,这要再过一两天,他就能重返工作岗位了。为了社会的和谐发展,我忍痛的出了一次血。在与同志们分别的这段日子,我借了另外的一个悲剧手机--每发一条短信就要重启一次。