Author Archives: verynix

Page 1 of 2112345...1020...Last »

CocoaPods安装ENOENT错误

ENOENT错误:

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/pathname.rb:422:in 'open': No such file or directory - /Users/test/.cocoapods/repos (Errno::ENOENT)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/pathname.rb:422:in 'foreach'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/pathname.rb:422:in 'children'
from /Library/Ruby/Gems/2.0.0/gems/cocoapods-0.35.0/lib/cocoapods/sources_manager.rb:63:in 'all'
from /Library/Ruby/Gems/2.0.0/gems/cocoapods-0.35.0/lib/cocoapods/user_interface/error_report.rb:130:in 'repo_information'
from /Library/Ruby/Gems/2.0.0/gems/cocoapods-0.35.0/lib/cocoapods/user_interface/error_report.rb:34:in 'report'
from /Library/Ruby/Gems/2.0.0/gems/cocoapods-0.35.0/lib/cocoapods/command.rb:58:in 'report_error'
from /Library/Ruby/Gems/2.0.0/gems/claide-0.7.0/lib/claide/command.rb:300:in 'handle_exception'
from /Library/Ruby/Gems/2.0.0/gems/claide-0.7.0/lib/claide/command.rb:274:in 'rescue in run'
from /Library/Ruby/Gems/2.0.0/gems/claide-0.7.0/lib/claide/command.rb:264:in 'run'
from /Library/Ruby/Gems/2.0.0/gems/cocoapods-0.35.0/lib/cocoapods/command.rb:45:in 'run'
from /Library/Ruby/Gems/2.0.0/gems/cocoapods-0.35.0/bin/pod:43:in `'
from /usr/bin/pod:23:in 'load'
from /usr/bin/pod:23:in 'main'

 

建议:

sudo rm -rf ~/.cocoapods/
pod setup

 

ruby gems 更改源

因为众所周知的原因,gem update,install等命令经常会遇到超时timeout,半天无响应,卡住等问题,建议换成淘宝的镜像源:

$ gem sources --remove https://rubygems.org/
$ gem sources -a https://ruby.taobao.org/
$ gem sources -l
*** CURRENT SOURCES ***

https://ruby.taobao.org
# 请确保只有 ruby.taobao.org
$ gem install rails

 

 

参考资料:http://ruby.taobao.org/

ubuntu 12.04安装jekyll

ubuntu版本

$ cat /etc/issue
Ubuntu 12.04 LTS \n \l

$ uname -a
Linux ubuntu 3.2.0-23-generic-pae #36-Ubuntu SMP Tue Apr 10 22:19:09 UTC 2012 i686 i686 i386 GNU/Linux

安装ruby和jekyll

没有gem就安装。

调整为taobao源

$ gem sources --remove https://rubygems.org/
$ gem sources --remove http://rubygems.org/
$ gem sources -a https://ruby.taobao.org/
$ gem sources -l
\*** CURRENT SOURCES ***

https://ruby.taobao.org
# 请确保只有 ruby.taobao.org

安装rvm和ruby:

$ sudo apt-get install curl
$ sudo gem install rvm
$ \curl -sSL https://get.rvm.io | bash
$ source /home/user/.rvm/scripts/rvm
$ rvm install ruby-2.1.2
$ rvm use ruby-2.1.2

安装js runtime:

$ sudo apt-get install nodejs
$ gem install execjs

安装jekyll:

$ gem install jekyll

备注: 由于apt-get安装的软件可能版本相对较老,因此推荐用rvm安装ruby。

可能遇到的一些问题

ERROR: Failed to build gem native extension

错误情况:

$ sudo gem install jekyll
Fetching: liquid-2.6.1.gem (100%)
Fetching: kramdown-1.4.1.gem (100%)
Fetching: mercenary-0.3.4.gem (100%)
Fetching: safe_yaml-1.0.3.gem (100%)
Fetching: colorator-0.1.gem (100%)
Fetching: yajl-ruby-1.1.0.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing jekyll:
        ERROR: Failed to build gem native extension.

        /usr/bin/ruby1.9.1 extconf.rb
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- mkmf (LoadError)
        from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
        from extconf.rb:1:in `<main>'


Gem files will remain installed in /var/lib/gems/1.9.1/gems/yajl-ruby-1.1.0 for inspection.
Results logged to /var/lib/gems/1.9.1/gems/yajl-ruby-1.1.0/ext/yajl/gem_make.out

原因: ruby版本过老,apt-get install ruby的版本一般是1.8,请使用rvm安装2.1版本的ruby;哪些最新可用版本请用rvm list known查看。

Could not find a JavaScript runtime

错误情况:

$ jekyll new myblog
/home/user/.rvm/gems/ruby-2.1.2/gems/execjs-2.2.1/lib/execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
        from /home/user/.rvm/gems/ruby-2.1.2/gems/execjs-2.2.1/lib/execjs.rb:5:in `<module:ExecJS>'
        from /home/user/.rvm/gems/ruby-2.1.2/gems/execjs-2.2.1/lib/execjs.rb:4:in `<top (required)>'
        from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/user/.rvm/gems/ruby-2.1.2/gems/coffee-script-2.3.0/lib/coffee_script.rb:1:in `<top (required)>'
        from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/user/.rvm/gems/ruby-2.1.2/gems/coffee-script-2.3.0/lib/coffee-script.rb:1:in `<top (required)>'
        from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/user/.rvm/gems/ruby-2.1.2/gems/jekyll-coffeescript-1.0.0/lib/jekyll-coffeescript.rb:2:in `<top (required)>'
        from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/user/.rvm/gems/ruby-2.1.2/gems/jekyll-2.3.0/lib/jekyll/deprecator.rb:46:in `block in gracefully_require'
        from /home/user/.rvm/gems/ruby-2.1.2/gems/jekyll-2.3.0/lib/jekyll/deprecator.rb:44:in `each'
        from /home/user/.rvm/gems/ruby-2.1.2/gems/jekyll-2.3.0/lib/jekyll/deprecator.rb:44:in `gracefully_require'
        from /home/user/.rvm/gems/ruby-2.1.2/gems/jekyll-2.3.0/lib/jekyll.rb:141:in `<top (required)>'
        from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/user/.rvm/gems/ruby-2.1.2/gems/jekyll-2.3.0/bin/jekyll:6:in `<top (required)>'
        from /home/user/.rvm/gems/ruby-2.1.2/bin/jekyll:23:in `load'
        from /home/user/.rvm/gems/ruby-2.1.2/bin/jekyll:23:in `<main>'
        from /home/user/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `eval'
        from /home/user/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `<main>'

错误原因: 未安装相关js库,请安装nodejs和execjs,请参照以上步骤。

注意事项

安装步骤是针对ubuntu 12.04,其他版本的ubuntu可能有最新的ruby,也可能没有,没经过测试,仅作参考。

理解http中的chunk编码

概述

http的chunk编码(分块传输编码)是HTTP/1.1(RFC2616)中新型的传输编码,是为了解决服务器动态产生的内容的传输问题. 一般情况下,HTTP的response是整体发送给客户端的,头中的Content-Length表示这个消息体的长度.数据的长度很重要,因为这直接决定了客户端如何正确解析返回的http response(应答). http的chunk编码就是把这一个整体的response分成多块数据进行传输,每块数据都有数据长度和具体的数据,最后一个数据块是0;具体的格式下面解释.

符号和名词定义

这里使用扩展的巴科斯范式(Augmented BNF)来描述下面的一些正式用语和语法.

chunk:分块;
CR = <US-ASCII CR, carriage return (13)>,ASCII表中的归位键;
LF = <US-ASCII LF, linefeed (10)>,ASCII表中的换行键;
CRLF = CR LF,一般是"\r\n";
HEX  = "A" | "B" | "C" | "D" | "E" | "F"
			| "a" | "b" | "c" | "d" | "e" | "f" | DIGIT |
OCTET = <any 8-bit sequence of data>

*rule,零条或者多条rule;完全的表达式"<n>*<m>rule",最少n条,之多m条rule,例如"*(rule)",代表任意数目的rule,包括0;"1*(rule)"代表至少有1条rule;"1*2rule",允许1条或者2条rule.

[rule],代表此条rule是可选的,例如"[foo bar]",等价于"*1(foo bar)";
"literal",字符串"literal",除非特别声明,要不然默认大小写敏感;

rule1 | rule2,或者的关系,rule1或者rule2.

英文版:

CR             = <US-ASCII CR, carriage return (13)>
LF             = <US-ASCII LF, linefeed (10)>
CRLF           = CR LF
HEX            = "A" | "B" | "C" | "D" | "E" | "F"
                      | "a" | "b" | "c" | "d" | "e" | "f" | DIGIT
OCTET          = <any 8-bit sequence of data>

*rule
      The character "*" preceding an element indicates repetition. The
      full form is "<n>*<m>element" indicating at least <n> and at most
      <m> occurrences of element. Default values are 0 and infinity so
      that "*(element)" allows any number, including zero; "1*element"
      requires at least one; and "1*2element" allows one or two.

[rule]
      Square brackets enclose optional elements; "[foo bar]" is
      equivalent to "*1(foo bar)".

rule1 | rule2
      Elements separated by a bar ("|") are alternatives, e.g., "yes |
      no" will accept yes or no.

更多请看 http://tools.ietf.org/html/rfc2616#section-2.2

格式

非正式格式:

chunk消息体 = (零个或者多个chunk) + 最后一个chunk + trailer + CRLF

正式格式:

Chunked-Body   = *chunk
                 last-chunk
                 trailer
                 CRLF

chunk          = chunk-size [ chunk-extension ] CRLF
                 chunk-data CRLF
chunk-size     = 1*HEX
last-chunk     = 1*("0") [ chunk-extension ] CRLF

chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
chunk-ext-name = token
chunk-ext-val  = token | quoted-string
chunk-data     = chunk-size(OCTET)
trailer        = *(entity-header CRLF)

解释:一个chunk编码块是由0个或者多个chunk块组成,每个chunk块有长度和数据,并且结束块是0,最后加一个CRLF;trailer很少见,可以不管.

例子

数据包中的数据:

4\r\n
Wiki\r\n
5\r\n
pedia\r\n
E\r\n
 in\r\n
\r\n
chunks.\r\n
0\r\n
\r\n

解释:第一个数据块长度为4,数据内容为”Wiki”;第二个数据块长度为5,数据内容为”pedia”;第三个数据块长度为E(十六进制,十进制是14),数据内容为” in\r\n\r\nchunks.”;”0\r\n”是最后一个数据块.

浏览器显示的数据:

Wikipedia in

chunks.

一个发chunk编码的简单http server

code

代码贴在github gist: https://gist.github.com/inix/d682e4e72cb6aea16132

use IO::Socket::INET;
$| = 1;

#response头部
my $response_hdr = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nServer: perl tcp server\r\nTransfer-Encoding: chunked\r\nConnection: Keep-Alive\r\n\r\n";

#创建socket,并且监听8888端口
$socket = new IO::Socket::INET (
LocalHost => '0.0.0.0',
LocalPort => '8888',
Proto => 'tcp',
Listen => 5,
Reuse => 1
) or die "ERROR in Socket Creation : $!\n";

while(1)
{
    #循环等待client发完请求
    $client_socket = $socket->accept();
    $request = "";
    while(defined($bf = <$client_socket>)) {
        $request.=$bf;

        #两个\r\n代表请求头发完,详细请阅读rfc2616
        if ($request =~ /\r\n\r\n/ and $request =~ /GET/) {
            print "we got request \"$request\"\n";
            last;
        }
    }

    #开始返回response给client
    print $client_socket "$response_hdr"; #打印头
    for(my $i = 0;$i < 5;$i ++){ #打印5个数据块,每个数据块长度是6,数据块内容为"1</br>"
        print $client_socket "6\r\n1</br>\r\n";
    }
    print $client_socket "0\r\n\r\n"; #打印最后的一个数据块

    # notify client that response has been sent
    shutdown($client_socket, 1);
}

$socket->close();

运行代码:

perl ./chunk-tcp-server.pl

访问:

用telnet:

http-chunk-telnet-req.png

用浏览器: http-chunk-ie-req.png 请注意两者的 不同点

FAQ

1.http的chunk传输编码只有HTTP/1.1支持吗?

是的.只有HTTP 1.1支持这种编码,HTTP 1.0不支持这种传输编码,而且RFC2616说,HTTP 1.1的程序必须支持这种编码,详见:http://tools.ietf.org/html/rfc2616#section-3.6.1

2.如果HTTP 1.0中出现”Transfer-Encoding: chunked”,会发生什么情况?

出现编码错误.有两种情况:request和response其中之一为1.0,request和response都是1.0. 编码错误,如果是明文,我们能从浏览器看到chunk的长度大小。稍微修改一下我们的程序,把$response\_Hdr中的”Http/1.1″变为”HTTP/1.0″,用IE访问,如下图所示: http-chunk-ie-resp10.png 可以看到chunk的长度都被显示出来了。 如果非明文,那么浏览器有可能无法显示内容,并且报告内容编码错误。

3.”Transfer-Encoding”和”Content-Encoding”有什么区别?

“Transfer-Encoding”是传输属性,即以什么方式传输,一般就是”Transfer-Encoding: chunked”,response的内容可以是明文,也可以是压缩的内容;而”Content-Encoding”是response消息以什么方式编码送给客户端,通常用于说明response的压缩编码方式,比如”Content-Encoding: gzip”或者”Content-Encoding: deflate”,告诉客户端response是用gzip和deflate格式压缩的,而经过压缩后,他们可以用”Content-Length”固定长度的方式传输,也可以用”Transfer-Encoding: chunked”分块的方式传输给客户端。

解决freebsd升级到最新ports遇到的问题

系统环境

uname -a
FreeBSD AN 7.0-RELEASE FreeBSD 7.0-RELEASE #0: Wed Jul 31 16:20:23 CST 2013   amd64

更新系统的ports tree

先su获取root权限:

portsnap fetch
portsnap extract

出错

安装vim:

cd /usr/ports/editors/vim
make install clean

出错:

Unknown modifier 't'

Unknown modifier 't'

Unknown modifier 't'

Unknown modifier 't'

"Makefile", line 137: Malformed conditional (${PYTHON_REL} < 300)
"Makefile", line 142: if-less endif
Unknown modifier 't'

Unknown modifier 't'

Unknown modifier 't'

Unknown modifier 't'

Unknown modifier 't'

"/usr/ports/Mk/bsd.sites.mk", line 953: Malformed conditional (!empty(_PERL_CPAN_ID) && ${_PERL_CPAN_FLAG:tl} == "cpan")
Unknown modifier 't'

Unknown modifier 't'

Unknown modifier 't'

Unknown modifier 't'

"/usr/ports/Mk/bsd.port.mk", line 2877: Unclosed conditional/for loop
"/usr/ports/Mk/bsd.port.mk", line 2877: Unexpected end of file in for loop.

"/usr/ports/Mk/bsd.port.mk", line 6590: Unclosed conditional/for loop
"/usr/ports/Mk/bsd.port.mk", line 6590: Unexpected end of file in for loop.

1 open conditional:
         at line 1177 (evaluated to true)
make: fatal errors encountered -- cannot continue

谷歌查了一下,原因是7.0这个release不是一个正在维护的版本了,更新后的ports tree和当前的版本不兼容。

解决办法

  • 升级当前系统到最新版本
  • 重新恢复port tree到当前对应版本

stackoverflow的一篇帖子给了我灵感,到freebsd的找到了ports tree的官方备份网站找到了压缩包,恢复之: 用root用户下载install.sh和ports.tgz到/root文件夹,执行脚本解压

sh ./install.sh

FreeBSD源代码安装tsung

我之前的一篇文章《FreeBSD安装tsung》介绍过在FreeBSD下用ports安装erlang和tsung,但是FreeBSD里面的软件太古老,很难安装成功,特别是安装java遇到很大的挫折,之后又尝试源代码安装erlang和tsung,发现一般情况下可以去掉java选项,可以使安装过程变得简单很多。

1.OS版本

64位的Freebsd,uname -a命令:

FreeBSD AN 7.0-RELEASE FreeBSD 7.0-RELEASE #2: Fri Apr 11 10:34:32 CST 2014   amd64

2.安装gmake

需要root权限:

cd /usr/ports/devel/gmake/
make install clean

2.安装erlang

版本:otp-src-R16B03.tar.gz,“gmake install”需要root权限:

tar zxvf otp_src_R16B03.tar.gz
cd otp_src_R16B03 && ./configure --without-javac && gmake && gmake install

3.安装tsung

版本:tsung-1.5.0.tar.gz,“gmake install”需要root权限:

tar zxvf tsung-1.5.0.tar.gz
cd tsung-1.5.0 && ./configure && gmake && gmake install

4.参考文献

推荐一些零碎的感觉有用的资源

零碎的推荐一些个人觉得有用的资源,以后经常更新。

coursera

创办于2012年3月的coursera是近年来最负盛名的网上公开课,汇集了大量来自斯坦福大学,普林斯顿大学,密歇根大学,伯克利大学,宾夕法尼亚大学等等全世界顶尖高等学府的大量课程。 比如计算机学科的算法分析,Andrew Ng的机器学习,密码学等; 数学学科的微积分,复分析,统计学等; 经济和金融学的微观经济学,非理性行为学,博弈论等,相当地优秀.相比较其他公开课来说,coursera在课堂作业,论坛,老师答疑等方面做得相当出色,强烈推荐之;爱学习的你,一定不要错过这个网站,实在是居家旅行必备,你值得拥有.

网易公开课

open.163.com,国内目前最好的公开课平台,推荐,目的以自学为主,没有coursera那样多的课堂练习,作业和论坛,课程也算多,不过有些课程没翻译完,推荐.

stackoverflow

stackoverflow,程序员问答网站,以其高质量的问答而闻名,一般的问题在几分钟之内就能得到满意的答案,前提是不要重复提问,要不然会被删贴.

dr.math

Dr. Math,非著名网站,但是如果你正在自学数学,遇到难题而苦于没有老师答疑,那么这里一定适合你,你的提问会有一位高学历的数学老师给你答疑,质量很高,推荐.

在线latex

code cogs latex,来自code cogs的在线latex编辑工具,将可视化的符号转换成latex代码,不想自己动手写latex的你,一定需要它.

github

github.com,社会化代码开源网站,以分布式版本管理工具git作为部署工具,上面有很多大量优秀的开源项目,推荐.

Page 1 of 2112345...1020...Last »