小编fea*_*ool的帖子

如何在Ruby中编写简单的HTTPS代理服务器?

我已经看过几个在Ruby中编写HTTP代理的例子,例如Torsten Becker的这个要点,但我如何扩展它以处理HTTPS,又称为"中间人"SSL代理?

我正在寻找一个简单的源代码框架,我可以扩展它以满足我自己的日志记录和测试需求.

更新

我已经使用了Charles,一个类似于Fiddler 的漂亮的HTTPS代理应用程序,它本质上是我想要的,除了它在一个应用程序中打包.我想写自己的,因为我有过滤和演示的特定需求.

更新II

四处乱逛,我理解术语好一点.我不是在完整的"中间人"SSL代理之后.相反,它将在我的机器上本地运行,因此我可以尊重它提供的任何SSL证书.但是,我需要查看我的请求的数据包的解密内容和响应的解密内容.

ruby ssl https proxy

6
推荐指数
2
解决办法
6512
查看次数

两个无符号整数之差的数据类型是什么?

我以前一直认为这是理所当然的,但假设我有:

uint8_t a;
uint8_t b;

if ((a - b) < 0) {
  ...
}
Run Code Online (Sandbox Code Playgroud)

表达式的数据类型是什么(a - b)Godbolt 先生告诉我这是一个带符号的值;这是由任何 C 规范保证的吗?

修正案:

(a-b)我现在明白,当 a 和 b 小于 int 时,类型提升将保证是int 。如果 andabsunsigned int呢?

unsigned int a;
unsigned int b;

if ((a - b) < 0) {
  ...
}
Run Code Online (Sandbox Code Playgroud)

c types language-lawyer

6
推荐指数
1
解决办法
131
查看次数

如何使用RSpec测试delayed_job回调挂钩

我想验证delayed_job回调挂钩是否被调用,但我没有看到如何让RSpec执行它,特别是当涉及几层类时.

假设我有一个像这样的ActiveRecord模型:

class MyJob < ActiveRecord::Base
  def perform
    # do some stuff
  end
  def after(job)
    # called by delayed_job after perform completes
  end
end
Run Code Online (Sandbox Code Playgroud)

从概念上讲,我希望沿着这些方向进行RSpec测试(虽然我知道这不正确):

it 'should call the :after callback hook' do
  my_job = MyJob.create
  my_job.should_receive(:after).with(an_instance_of(Delayed::Backend::ActiveRecord::Job))

  Delayed::Job.enqueue(my_job)
  Delayed::Worker.new.work_off
end
Run Code Online (Sandbox Code Playgroud)

我仔细研究了stackoverflow,relishapp.com/rspec以及我能想到的所有其他地方.这不可能太难了吧?(我打赌@zetetic在睡梦中知道答案......;)

(警告:我的实际案例使用MyJob和DJ之间的"垫片"课程.这个简单案例的答案可能会触发更复杂的后续行动!)

rspec ruby-on-rails delayed-job

5
推荐指数
1
解决办法
1751
查看次数

Ruby On Rails中对AJAX的一个很好的介绍是什么?

我需要在Ruby On Rails应用程序的上下文中对AJAX有一些很好的介绍性指针.

这是独家新闻:我的应用程序使用Delayed :: Job旋转了几个任务.虽然任务正在忙于从外部站点传播数据,但我希望让用户对其进度进行评估.我不想刷新整个页面 - 只是页面上的一个矩形,每个任务都可以显示状态消息.

我不相信我需要一个完整的推送技术 - 只要bg任务正在运行,使用javascript启动的轮询来更新屏幕就足够了.

对于AJAX来说这是一个合适的场景(对吧?),我认为Rails3有特定的结构来支持AJAX-y与浏览器的交互(对吧?).Ryan Bates的Railscast on Polling For Changes对于这个具体案例来说是一个很好的模板,但是我想要更好地了解幕后真正发生的事情.好的,那我从哪里开始呢?

为了它的价值,我已经在我的Rails3应用程序中使用了jQuery.

ajax jquery ruby-on-rails

5
推荐指数
1
解决办法
226
查看次数

在Procfile中启动多个worker(开发环境vs Heroku)

我有一个简单的Procfile,内容如下:

web: bundle exec rails server thin -p $PORT
worker: bundle exec rake jobs:work
Run Code Online (Sandbox Code Playgroud)

在Heroku上,这将启动N个工作任务,其中N是我将其缩放的任何东西.

在我的开发系统上,

$ foreman start
Run Code Online (Sandbox Code Playgroud)

只会启动一个工作任务.如果我想启动三个worker,我需要一个看起来像这样的Procfile:

web: bundle exec rails server thin -p $PORT
worker: bundle exec rake jobs:work
worker: bundle exec rake jobs:work
worker: bundle exec rake jobs:work
Run Code Online (Sandbox Code Playgroud)

这是一个有点迂腐的问题,但如果我希望我的开发环境像我的Heroku环境一样,那么启动N个工作任务的最佳方法是什么?是批准的方法来创建(例如)Procfile_local并通过它使用它foreman -f Procfile_local

ruby-on-rails heroku delayed-job foreman

5
推荐指数
1
解决办法
3949
查看次数

如何使用普通控制器测试来测试"捕获所有路线"?

注意:根据RafaeldeF.Ferreira的建议,这个问题自其原始形式以来经过大量编辑.

我的基于JSON的应用程序需要在给出错误路径时返回合理的内容.我们已经知道以下rescue_from ActionController::RoutingError在Rails 3.1和3.2中不起作用:

# file: app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  protect_from_forgery
  rescue_from ActionController::RoutingError, :with => :not_found
  ...
end
Run Code Online (Sandbox Code Playgroud)

(这在https://github.com/rails/rails/issues/671中有详细记载.)所以我实现了JoséValim在本博客条目(第3项)中描述的内容,详情如下.

但测试它一直存在问题.这个控制器rspec测试:

# file: spec/controllers/errors_controller.rb
require 'spec_helper'
require 'status_codes'
describe ErrorsController do
  it "returns not_found status" do
    get :not_found
    response.should be(StatusCodes::NOT_FOUND)
  end
end
Run Code Online (Sandbox Code Playgroud)

失败了:

ActionController::RoutingError: No route matches {:format=>"json", :controller=>"sites", :action=>"update"}
Run Code Online (Sandbox Code Playgroud)

然而,这个集成测试调用ErrorsController#not_found并成功:

# file: spec/requests/errors_spec.rb
require 'spec_helper'
require 'status_codes'

describe 'errors service' do
  before(:each) do
    @client = FactoryGirl.create(:client)
  end

  it "should catch routing error and …
Run Code Online (Sandbox Code Playgroud)

routing actioncontroller ruby-on-rails-3

5
推荐指数
0
解决办法
596
查看次数

为什么我会使用虚拟和具体的类获得"未定义的符号... typeinfo ... vtable"?

我正在重新学习C++(意思是:温柔地对待我!:).我有一个Node带有抽象方法(step())的超类(),它必须在子类(TestNode)中实现.它编译没有错误,没有任何警告,但链接它导致:

bash-3.2$ g++ -Wall -o ./bin/t1 src/t1.cpp
Undefined symbols for architecture x86_64:
  "typeinfo for test::Node", referenced from:
      typeinfo for test::TestNode in t1-9f6e93.o
  "vtable for test::Node", referenced from:
      test::Node::Node() in t1-9f6e93.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)

据我所知,我已经定义了"第一个非内联虚拟成员函数"(即TestNode::step()).

我已经仔细阅读错误消息,我读过的博客文章在这里,并期待其他一些SO职位( …

c++ clang++

5
推荐指数
2
解决办法
9751
查看次数

Chrome“获取堆快照”从未完成

概括:

我正在尝试使用 Chrome 的“获取堆快照”来追踪某些 Node.js 代码中的内存泄漏,但获取快照的操作永远不会完成。

我是否以某种方式错误地使用了该工具?

细节:

这是 Node.js 应用程序:

#!/usr/bin/env node

var serialserver = require('./p5.serialserver');
serialserver.start();
console.log("p5.serialserver is running");
Run Code Online (Sandbox Code Playgroud)

以下是我调用它的方式以及它打印出来的内容:

$ node --inspect ./p5serial
Debugger listening on port 9229.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
     chrome-devtools://devtools/remote/serve_file/@521e5b7e2b7cc66b4006a8a54cb9c4e57494a5ef/inspector.html?experiments=true&v8only=true&ws=localhost:9229/node
p5.serialserver is running
Run Code Online (Sandbox Code Playgroud)

在 Chrome(版本 59.0.3071.115)中,在 OSX 10.12.6 (Sierra) 下运行,我打开给定的 URL 并单击Take Heap Snapshot。我在左栏中看到图标,上面写着:

Snapshot 1
Snapshotting...
Run Code Online (Sandbox Code Playgroud)

……但等了十分钟,快照还没有完成。这不是一个特别大的节点应用程序。

我缺少什么?

memory-leaks google-chrome node.js google-chrome-devtools

5
推荐指数
1
解决办法
2843
查看次数

在pandas/numpy中实现分段函数的正确方法

我需要创建一个传递给的函数curve_fit.在我的例子中,函数最好定义为分段函数.

我知道以下内容不起作用,但我正在展示它,因为它使函数的意图清晰:

def model_a(X, x1, x2, m1, b1, m2, b2):
    '''f(x) has form m1*x + b below x1, m2*x + b2 above x2, and is
    a cubic spline between those two points.'''
    y1 = m1 * X + b1
    y2 = m2 * X + b2
    if X <= x1:
        return y1    # function is linear below x1
    if X >= x2:
        return y2    # function is linear above x2
    # use a cubic spline to interpolate between …
Run Code Online (Sandbox Code Playgroud)

python numpy scipy pandas

5
推荐指数
1
解决办法
554
查看次数

分发关键字参数的最佳方式?

在被调用函数之间分配关键字参数的批准编程模式是什么?

考虑这个人为(和错误)的例子:

def create_box(**kwargs):
    box = Box()
    set_size(box, **kwargs)
    set_appearance(box, **kwargs)

def set_size(box, width=1, height=1, length=1):
    ...

def set_appearance(box, material='cardboard', color='brown'):
    ...
Run Code Online (Sandbox Code Playgroud)

显然,set_size()方法会反对接收materialcolor关键字参数,正如set_appearance()会反对接收widthheightlength参数。

有一个有效的参数create_box()应该使所有关键字和默认值都显式,但明显的实现是相当笨拙的:

def create_box(width=1, height=1, length=1, material='cardboard', color='brown'):
    box = Box()
    set_size(box, width=width, height=height, length=length)
    set_appearance(box, material=material, color=color)
Run Code Online (Sandbox Code Playgroud)

有没有更 Pythonic 的方法来解决这个问题?

python keyword-argument

5
推荐指数
1
解决办法
76
查看次数