在“验证"中正确使用 HTTP 状态码服务器 [英] Proper use of HTTP status codes in a "validation" server

查看:21
本文介绍了在“验证"中正确使用 HTTP 状态码服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序发送到第三方 SOA 服务器的数据中,有复杂的 XML.服务器所有者确实提供了 XML 模式 (.xsd),并且由于服务器拒绝无效的 XML 并发送无意义的消息,因此我需要在发送之前在本地验证它们.

Among the data my application sends to a third-party SOA server are complex XMLs. The server owner does provide the XML schemas (.xsd) and, since the server rejects invalid XMLs with a meaningless message, I need to validate them locally before sending.

我可以使用独立的 XML 模式验证器,但它们很慢,主要是因为解析模式文件需要时间.因此,我以 HTTP Server 的形式编写了自己的模式验证器(如果需要,用 Java),它缓存已解析的模式.

I could use a stand-alone XML schema validator but they are slow, mainly because of the time required to parse the schema files. So I wrote my own schema validator (in Java, if that matters) in the form of an HTTP Server which caches the already parsed schemas.

问题是:在验证过程中,很多事情都可能出错.除了意外异常和成功验证:

The problem is: many things can go wrong in the course of the validation process. Other than unexpected exceptions and successful validation:

  • 服务器可能找不到指定的架构文件
  • 指定的文件可能不是有效的架构文件
  • XML 对架构文件无效

由于它是一个 HTTP 服务器,我想为客户端提供有意义的状态代码.对于上述所有情况,服务器是否应该以 400 错误(Bad request)回答?或者他们与 HTTP 无关,它应该用正文中的消息来回答 200?还有什么建议吗?

Since it's an HTTP Server I'd like to provide the client with meaningful status codes. Should the server answer with a 400 error (Bad request) for all the above cases? Or they have nothing to do with HTTP and it should answer 200 with a message in the body? Any other suggestion?

更新:主要应用程序是用Ruby编写的,它没有一个好的xml模式验证库,所以单独的验证服务器并没有过度设计.

Update: the main application is written in Ruby, which doesn't have a good xml schema validation library, so a separate validation server is not over-engineering.

推荐答案

将验证过程中的错误情况映射到有意义的 HTTP 状态代码是一种完全有效的想法.

It's a perfectly valid thinking to map error situations in the validation process to meaningful HTTP status codes.

我假设您使用 URI 将 XML 文件作为 POST 内容发送到验证服务器以确定特定的验证模式.

I suppose you send the XML file to your validation server as a POST content using the URI to determine a specific schema for validation.

所以这里有一些关于错误映射的建议:

So here are some suggestions for error mappings:

  • 200:XML 内容有效
  • 400:XML 内容格式不正确,标头不一致,请求与 RFC 2616 语法不匹配
  • 401:在缓存中未找到架构,服务器需要凭据用于针对第 3 方 SOA 后端的身份验证才能获取架构文件
  • 404:未找到架构文件
  • 409:XML 内容对指定的架构无效
  • 412:指定的文件不是有效的 XMl 架构
  • 500:验证服务器中的任何意外异常(NullPointerExceptions 等)
  • 502:在缓存中找不到架构,尝试从第 3 方 SOA 服务器请求它失败.
  • 503:验证服务器正在重新启动
  • 504:查看 502,原因为超时

这篇关于在“验证"中正确使用 HTTP 状态码服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆