我正在开发一个使用 JWT 身份验证和 websockets 的小型 .net 核心应用程序。
我已成功实现为标准 Web api 控制器生成和验证令牌。但是,我也想验证WebSocket请求的令牌,这当然不适用于[Authorize]属性。
我已经像这样设置了我的中间件管道:
app.UseWebSockets();
app.Use(async (http, next) => {
if (http.WebSockets.IsWebSocketRequest == false) {
await next();
return;
}
/// Handle websocket request here. How to check if token is valid?
});
// secretKey contains a secret passphrase only your server knows
var secretKey = .....;
var signKey = new SigningCredentials (
new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secretKey)),
SecurityAlgorithms.HmacSha256
);
var tokenValidationParameters = new TokenValidationParameters {
ValidateIssuer = false,
ValidateAudience = false, …Run Code Online (Sandbox Code Playgroud) 如果我在函数中将互斥锁声明为静态并使用该互斥锁来锁定某个变量.这个互斥体是否在线程之间"共享",例如我可以使用一些更干净的代码逃脱?
将字符串附加到双指针char类型的示例,我想从多个线程调用这样的东西:
void func(char *msg) {
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static char **buffer;
static unsigned int i=0;
pthread_mutex_lock(&mtx);
buffer = realloc(++i * sizeof(char *));
buffer[i-1] = realloc(strlen(msg) + 1);
strcpy(buffer[i-1], msg);
pthread_mutex_unlock(&mtx);
return;
}
Run Code Online (Sandbox Code Playgroud) 所以我最近在c ++上看了这个演讲:https: //www.youtube.com/watch?v = mFUXNMfaciE
我对尝试它非常感兴趣.因此,在一些玩具程序之后,我被困在如何正确地将矢量矢量平面化为矢量.根据这里的文档:https://ericniebler.github.io/range-v3/这是可能的使用ranges::view::for_each.但是我似乎无法让它发挥作用.这是一些最小的代码.
#include <range/v3/all.hpp>
#include <iostream>
#include <vector>
int main()
{
auto nums = std::vector<std::vector<int>>{
{0, 1, 2, 3},
{5, 6, 7, 8},
{10, 20},
{30},
{55}
};
auto filtered = nums
| ranges::view::for_each([](std::vector<int> num) { return ranges::yield_from(num); })
| ranges::view::remove_if([](int i) { return i % 2 == 1; })
| ranges::view::transform([](int i) { return std::to_string(i); });
for (const auto i : filtered)
{
std::cout << i << …Run Code Online (Sandbox Code Playgroud) 我正在编写一个应用程序,需要打开另一个进程并获得它的输出.在线我无处不在,我必须使用popen并从文件中读取.
但我无法从中读到.命令的输出将输出到调用应用程序的控制台窗口中.以下是我正在使用的代码.我添加了一些打印来调试.
#include <string>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <array>
int main()
{
// some command that fails to execute properly.
std::string command("ls afskfksakfafkas");
std::array<char, 128> buffer;
std::string result;
std::cout << "Opening reading pipe" << std::endl;
FILE* pipe = popen(command.c_str(), "r");
if (!pipe)
{
std::cerr << "Couldn't start command." << std::endl;
return 0;
}
while (fgets(buffer.data(), 128, pipe) != NULL) {
std::cout << "Reading..." << std::endl;
result += buffer.data();
}
auto returnCode = pclose(pipe);
std::cout << …Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的类型。
data State = Unsanitized | Sanitized
type family BarTy (s :: State) :: Type where
BarTy 'Unsanitized = String
BarTy 'Sanitized = Int
data Foo (a :: State) = Foo (Bar a)
data Bar (a :: State) = Bar (BarTy a)
Run Code Online (Sandbox Code Playgroud)
我使用 type 参数来更改某些字段的类型。例如,当通过验证管道演化数据结构时,这很有用。
现在我想编写一个可以从这个数据结构中提取一些东西的类,但它是哪个并不重要State。完整示例:
data State = Unsanitized | Sanitized
type family BarTy (s :: State) :: Type where
BarTy 'Unsanitized = String
BarTy 'Sanitized = Int
data Foo (a :: State) = Foo (Bar a) …Run Code Online (Sandbox Code Playgroud) 我有一个用 GADT 创建的数据结构,我想使用aeson. 但是类型检查器抱怨在所有情况下只能创建 GADT 的构造函数之一。看这个例子:
data Foo = Hello | World
data SFoo :: Foo -> Type where
SHello :: SFoo 'Hello
SWorld :: SFoo 'World
instance FromJSON (SFoo a) where
parseJSON = withText "Foo" \case
"hello" -> pure SHello
"world" -> pure SWorld
Run Code Online (Sandbox Code Playgroud)
所以我希望能够将“hello”字符串解析为SHello,将“world”字符串解析为SWorld. 类型检查器抱怨以下错误:
data Foo = Hello | World
data SFoo :: Foo -> Type where
SHello :: SFoo 'Hello
SWorld :: SFoo 'World
instance FromJSON (SFoo a) where
parseJSON …Run Code Online (Sandbox Code Playgroud) I'm relatively new to haskell so forgive me if this is really obvious.
Basically I have two Bool and based on them I want to choose the implementation of 3 different functions. In the case that both bools are equal (e.g. both True or both False) the functions should do nothing. Then there are different implementation if one or the other Bool is True.
These function involve constraints so for instance the first function has an Ord or Bounded constraint …
我最初是个人,但最近我开始在python中做一些事情.给我带来麻烦的是python中更先进的数据结构.
我可以用c中的多个列表来完成所有操作,但那会很无聊吗?
无论如何,我有一个数据结构,它基本上是一个dicts列表,其中dict的值字段是另一个2个键值对的列表:
clients = [
{'client1':[{'test':'testvalue','status':'statusvalue'}]},
{'client2':[{'test':'testvalue','status':'statusvalue'}]},
{'client3':[{'test':'testvalue','status':'statusvalue'}]}
]
Run Code Online (Sandbox Code Playgroud)
现在我希望能够访问testvalue和statusvalue字段并修改或读取它们.基于列表中的位置.
在伪代码中它会是这样的:
for i in range(0,clients):
getvalue(clients[i].'test')
setvalue(clients[i].'test')
getvalue(clients[i].'status')
setvalue(clients[i].'status')
Run Code Online (Sandbox Code Playgroud)
最后我想使用这个数据结构来渲染一个带有jinja2的html页面
我正在写一些小的python应用程序,它使用请求来获取和发布数据到html页面.
现在我遇到的问题是,如果我无法访问html页面,代码将停止并超出最大重试次数.如果我无法访问服务器,我希望能够做一些事情.
这样的事情可能吗?
这是示例代码:
import requests
url = "http://127.0.0.1/"
req = requests.get(url)
if req.status_code == 304:
#do something
elif req.status_code == 404:
#do something else
# etc etc
# code here if server can`t be reached for whatever reason
Run Code Online (Sandbox Code Playgroud) 我有一个Python脚本,必须调用某个应用3次。这些调用应该是并行的,因为它们需要几个小时才能完成,并且彼此之间不相干。但是它们的脚本应该停止运行,直到它们全部完成,然后再进行一些清理工作。
这是一些代码:
#do some stuff
for work in worklist: # these should run in parralel
output=open('test.txt','w')
subprocess.call(work,stdout=output,stderr=output)
output.close()
# wait for subprocesses to finish
# cleanup
Run Code Online (Sandbox Code Playgroud)
所以我基本上想在捕获其输出到文件中的同时运行此命令。完成所有实例后,我要继续执行脚本
我正在研究一个c ++代码库,它使用矩阵库来计算各种事物.其中之一是计算矩阵的逆.它使用高斯elimation来实现这一点.但结果非常不准确.因此,将逆矩阵与原始矩阵相乘甚至不会接近单位矩阵.
下面是用于计算逆的代码,矩阵是在数值类型和行和列上模板化的:
/// \brief Take the inverse of the matrix.
/// \return A new matrix which is the inverse of the current one.
matrix<T, M, M> inverse() const
{
static_assert(M == N, "Inverse matrix is only defined for square matrices.");
// augmented the current matrix with the identiy matrix.
auto augmented = this->augment(matrix<T, M, M>::get_identity());
for (std::size_t i = 0; i < M; i++)
{
// divide the current row by the diagonal element.
auto divisor = augmented[i][i];
for …Run Code Online (Sandbox Code Playgroud) c++ ×3
haskell ×3
python ×3
aeson ×1
asp.net ×1
asp.net-core ×1
c ×1
c# ×1
dictionary ×1
file ×1
gadt ×1
iterator ×1
list ×1
math ×1
matrix ×1
popen ×1
pthreads ×1
range-v3 ×1
subprocess ×1
text-parsing ×1